当我试图在我的库中为const和非const模板参数提供函数时,我遇到了一个奇怪的问题.以下源代码是一个最小的示例现象:
#include <iostream>
template<typename some_type>
struct some_meta_class;
template<>
struct some_meta_class<int>
{
typedef void type;
};
template<typename some_type>
struct return_type
{
typedef typename some_meta_class< some_type >::type test;
typedef void type;
};
template<typename type>
typename return_type<type>::type foo( type & in )
{
std::cout << "non-const" << std::endl;
}
template<typename type>
void foo( type const & in )
{
std::cout << "const" << std::endl;
}
int main()
{
int i;
int const & ciref = i;
foo(ciref);
}
Run Code Online (Sandbox Code Playgroud)
我试图为foo实现一个非const版本和一个const版本,但不幸的是这个代码不能在CLANG 3.0和gcc 4.6.3上编译.
main.cpp:18:22:错误:未定义模板'some_meta_class'的隐式实例化
因此,由于某种原因,编译器想要使用非const版本的foo作为const …