[C++ Template]what is the type of instantiation of template function.
2011-07-06 11:49
399 查看
The book <<C++ templates>> said:
however, in VS2008 and gcc 4.3, the compilers can deduce the type of parameter. The followed code has no compile error.
look here(http://ideone.com/yTpaJ ) for gcc result.
[align=left]#include <vector>[/align]
[align=left]#include <algorithm>[/align]
[align=left]using namespace std;[/align]
[align=left]template <typename T, int VAL>[/align]
[align=left]T addValue (T const& x) {[/align]
[align=left] return x + VAL;[/align]
[align=left]}[/align]
[align=left]int main(int argc, char* argv[])[/align]
[align=left]{[/align]
[align=left] vector<int> source;[/align]
[align=left] vector<int> dest;[/align]
[align=left] int (*funP)(int const&);[/align]
[align=left] funP = addValue<int,5>;[/align]
[align=left] std::transform (source.begin(), source.end(), // start and end of source[/align]
[align=left] dest.begin(), // start of destination[/align]
[align=left] addValue<int,5>); // operation[/align]
[align=left]
[/align]
[align=left] std::transform (source.begin(), source.end(), // start and end of source[/align]
[align=left] dest.begin(), // start of destination[/align]
[align=left] funP); [/align]
[align=left] return 0;[/align]
[align=left]}[/align]
however, in VS2008 and gcc 4.3, the compilers can deduce the type of parameter. The followed code has no compile error.
look here(http://ideone.com/yTpaJ ) for gcc result.
[align=left]#include <vector>[/align]
[align=left]#include <algorithm>[/align]
[align=left]using namespace std;[/align]
[align=left]template <typename T, int VAL>[/align]
[align=left]T addValue (T const& x) {[/align]
[align=left] return x + VAL;[/align]
[align=left]}[/align]
[align=left]int main(int argc, char* argv[])[/align]
[align=left]{[/align]
[align=left] vector<int> source;[/align]
[align=left] vector<int> dest;[/align]
[align=left] int (*funP)(int const&);[/align]
[align=left] funP = addValue<int,5>;[/align]
[align=left] std::transform (source.begin(), source.end(), // start and end of source[/align]
[align=left] dest.begin(), // start of destination[/align]
[align=left] addValue<int,5>); // operation[/align]
[align=left]
[/align]
[align=left] std::transform (source.begin(), source.end(), // start and end of source[/align]
[align=left] dest.begin(), // start of destination[/align]
[align=left] funP); [/align]
[align=left] return 0;[/align]
[align=left]}[/align]
相关文章推荐
- How to get a type in C++ when its template argument is the argument
- C++: How is the process of function calling in C++
- C++: How is the process of function calling in C++
- What is the role of the activation function in a neural network?
- C++: How is the process of function calling in C++
- Question 38: What is the output of the program below in C++?
- C++: How is the process of function calling in C++
- C++: How is the process of function calling in C++
- Is the type of “pointer-to-member-function” different from “pointer-to-function”?
- Linux and C[2]: What is the function of inet_aton or inet ntoa ?
- what is the mean of "GPRINT Type" in cacti
- Question 42: A C++ developer wants to explicitly specialize the template function below for the char * type:
- (NOT CLEAR)Question 44: What member function of std::fstream could a C++ developer invoke in order to change the target output f
- ReactJs 报错 Element type is invalid: expected a string (from built-in components) or a class/function (for composite components) but got: undefined. Check the render method of `Me`.
- C++: How is the process of function calling in C++
- C++: How is the process of function calling in C++
- Java出现No enclosing instance of type E is accessible. Must qualify the allocation with an enclosing
- VMware安装Ubuntu17.04的VMware-tools提示:what is the location of the "ifconfig" program on your machin问题解决
- The hierarchy of the type GreetingBeforeAdvice is inconsistent
- 继承“JdbcDaoSupport”后,报“The hierarchy of the type AccoutDaoImpl is inconsistent”的解决方案