出于可读性的原因,我想将一个函数模板专门化为接近在命名空间内声明的类的定义:
#include <iostream>
template<typename T> void my_function() {
std::cout << "my_function default" << std::endl;
}
namespace Nested {
class A {};
template<> void my_function<A>() {
std::cout << "my_function specialization for A" << std::endl;
}
}
但是,使用上面的代码,我从 clang++ 4.0 得到以下错误:
error: no function template matches function template specialization 'my_function'
这似乎是一个命名空间问题。我怎样才能使上述工作(不将模板函数专业化移出Nested
命名空间)?
编辑:我也尝试过添加::my_function
专业:
test.cpp: error: definition or redeclaration of 'my_function' cannot name the global scope
template<> void ::my_function<A>() {
~~^