我试图实现一个元程序,它会发现给定的指针类型是否const
存在。IE
is_const<TYPE*>::value
应该false
is_const<const TYPE*>::value
应该true
以下是代码:
template<class TYPE>
struct is_const
{
typedef char yes[3];
template<typename T>
struct Perform
{
static yes& check (const T*&);
static char check (T*&);
};
TYPE it;
enum { value = (sizeof(Perform<TYPE>::check(it)) == sizeof(yes)) };
};
编译器错误消息是:
In instantiation of ‘is_const<int*>’:
instantiated from here
error: no matching function for call to ‘is_const<int*>::Perform<int*>::check(int*&)’
note: candidates are: static char (& is_const<TYPE>::Perform<T>::check(const T*&))[3] [with T = int*, TYPE = int*]
note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]
我的重点已转移到错误消息上。如果你看到最后一行:
note: static char is_const<TYPE>::Perform<T>::check(T*&) [with T = int*, TYPE = int*]
如果我们真的替换T = int*
然后TYPE = int*
它真的应该匹配适当的函数(char check()
)。我很想知道这里出了什么问题。