Comments (8)
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.
我在想,既然您用模板,那么我直接用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.
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.
最后,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.
@Mooophy Awesome! 👍 I don't need to say anything more. 😺
from cppprimer.
~
from cppprimer.
@Mooophy Thank you for your advice .:+1:
from cppprimer.
@pezy Thank you for your answer in SegmentFault!:+1:
from cppprimer.
Related Issues (20)
- ex16_29 include error file,It compile failed HOT 2
- about 16.2.7 HOT 2
- Exercise 7.36
- Exercise 6.53
- Exercise 5.3 correct answer HOT 1
- Ex 6_8 HOT 1
- Ex 6_9 (added #include "fact.cpp")
- Ex 6_10 (added "return 0;")
- Exercise 13.37
- Answer to Exercises 3.37 is not entirely true.
- Ex16.49
- The new soulution of Exercise 5.12
- Ex.7.53 will not compile under the C++14 standard
- #include<exception>
- ex7_22.h 'class' instead of 'struct'
- Bug report exercise 16.12
- An error in exercise 16.28, in file <unique_pointer.h>
- Small modification for answer of Exercise 3.9 - mention `s` is a empty string
- Exercise 9.28(The loop may not stop)
- terminate called after throwing an instance of 'std::bad_alloc
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppprimer.