Giter VIP home page Giter VIP logo

Comments (8)

pezy avatar pezy commented on August 19, 2024

template
T abs(T i)
{
return i >= 0 ? i : -i;
}

This is template, out of this chapter. (sorry for that.)

But the question of exercise 6.5:

Exercise 6.5: Write a function to return the absolute value of its argument.

Doesn't clarify which type of the argument. so in my opinion, template is the only way. maybe @Mooophy can give some advices.


The other question about Visual Studio 2013, I will respond to you tomorrow. (I don't have windows at home.) 😅

from cppprimer.

Mooophy avatar Mooophy commented on August 19, 2024

我在想,既然您用模板,那么我直接用int替换成实体,这样算不算重复定义?vs2013编译的时候,第一次,提示错误,说重复定义,但是奇怪的是,第二次编译的时候竟然不报错了,不知道怎么回事,求解答!

I tried the codes below on vs2012 and qt creator with MinGW4.8.2. No gcc nor clang can be found at hand..

experiment I

#include <iostream>

template <typename T>
T abs(T i)
{
    std::cout << "template" << std::endl;
    return i >= 0 ? i : -i;
}

int abs(int i)
{
    std::cout << "non template" << std::endl;
    return i >= 0 ? i : -i;
}

int main()
{
    auto result = abs(-1);
    std::cout << result << std::endl;
    return 0;
}

Both compiled and output the same thing :

non template
1

So the non template version was called.

experiment II

#include <iostream>

template <typename T>
T abs(T i)
{
    std::cout << "template" << std::endl;
    return i >= 0 ? i : -i;
}

//int abs(int i)
//{
//    std::cout << "non template" << std::endl;
//    return i >= 0 ? i : -i;
//}

int main()
{
    auto result = abs(-1);
    std::cout << result << std::endl;
    return 0;
}

Output differed the previous try, but same on two compilers :

1

It's reasonable to guess that this time the library version abs was called.

experiment III

#include <iostream>

template <typename T>
T abs_def(T i)
//^^^^^^^<--note the function name had been modified.
{
    std::cout << "template" << std::endl;
    return i >= 0 ? i : -i;
}

//int abs(int i)
//{
//    std::cout << "non template" << std::endl;
//    return i >= 0 ? i : -i;
//}

int main()
{
    auto result = abs_def(-1);
    //            ^^^^^^^

    std::cout << result << std::endl;
    return 0;
}

output:

template
1

Finally the custom template version abs was called.

from cppprimer.

Mooophy avatar Mooophy commented on August 19, 2024

A summary copied from here

  • Nontemplate functions are first-class citizens. A plain old nontemplate function that matches the parameter types as well as any function template will be selected over an otherwise-just-as-good function template.
  • If there are no first-class citizens to choose from that are at least as good, then function base templates as the second-class citizens get consulted next.

For more detail please go through cp5 chapter 16

from cppprimer.

Mooophy avatar Mooophy commented on August 19, 2024

最后,int fact(int val)我在vs2013中是这么写的

#ifndef int fact(int val)
#endif

Pretty sure @Ocxs has used macro in a wrong way. Please read something like this.

Alternatively, you can try Qt Creator which generates header guards when new header file added. Visual Studio is a great tool, but not for beginners..

Hope helps.

from cppprimer.

pezy avatar pezy commented on August 19, 2024

@Mooophy Awesome! 👍 I don't need to say anything more. 😺

from cppprimer.

Mooophy avatar Mooophy commented on August 19, 2024

~

from cppprimer.

Ocxs avatar Ocxs commented on August 19, 2024

@Mooophy Thank you for your advice .:+1:

from cppprimer.

Ocxs avatar Ocxs commented on August 19, 2024

@pezy Thank you for your answer in SegmentFault!:+1:

from cppprimer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.