【发布时间】:2017-05-02 10:18:47
【问题描述】:
出于可读性的原因,我想将一个函数模板专门化为一个接近于在命名空间内声明的类的定义:
#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>() {
~~^
【问题讨论】:
-
试试
::my_function。 -
有一个提案:open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3730.html,但看起来它还没有被接受。但是,我的一位同事发现使用 clang 编译器执行类似
::my_function<A>()的操作非常有效
标签: c++ templates namespaces template-specialization explicit-specialization