C ++ Primer 5th Edition在第16.3章(讨论函数模板重载的章节)结尾处有一小段建议:
在定义任何函数之前,声明重载集中的每个函数 功能。这样你就不必担心编译器会不会 在看到您打算调用的函数之前实例化一个调用。
这是否告诉我,在重载解析期间选择候选和可行函数时,编译器可能会实例化一个最终未被选择的函数模板?我试着看看这是否真的会发生:
template<class> struct always_false : std::false_type {};
template <typename T> void test(T const &){
static_assert(always_false<T>::value, "If this fires, it is instantiated");
}
template <typename T> void test(T*) { }
int main(){
int *q = nullptr;
test(q); //test(T*) should be the best match
}
如果以任何形式实例化test(T const &)
,此程序将抛出编译器错误,除非程序按预期编译正常。那么什么样的编译事故就是小费试图阻止我?什么时候它会在看到我试图调用的函数之前实例化一个函数?
答案 0 :(得分:5)
作者警告你:
template<class> struct always_false : std::false_type {};
template <typename T> void test(T const &){
static_assert(always_false<T>::value, "If this fires, it is instantiated");
}
int main(){
int *q = nullptr;
test(q); //test(T*) will not be matched.
}
template <typename T> void test(T*)
{
}
这些:
template<class> struct always_false : std::false_type {};
template <typename T> void test(T const &){
static_assert(always_false<T>::value, "If this fires, it is instantiated");
}
template <> void test<int>(int const &);
void test(int *);
int main(){
int *q = nullptr;
test(q); //test(int*) should be the best match
int a;
test(a); // test<int>(int const&) should be the best match
}
template <> void test<int>(int const &)
{
}
void test(int *)
{
}
如果您不提供
的声明template <> void test<int>(int const &);
void test(int *);
在main
之前,他们不会在main
中匹配。
答案 1 :(得分:2)
我看到很多SO问题是
的一些变种template<class T, class... Ts>
T sum(T t, Ts... ts) { return t + sum(ts...); }
// ^ |
// |--------------------------------
// only one visible in
// definition context
template<class T>
T sum(T t) { return t; }
int main() {
sum(1, 2); // doesn't compile
}
(返回类型并不完美,但你明白了。)
当人们不编译时,人们会感到惊讶。
或者,更有趣,
template<class T> void f(T t) { f((int)t); }
void f(int) { /*...*/ }
int main() {
f(1L); // infinite recursion
}