我试图实现一个元程序,它发现给定的指针类型是否为const
。即为什么SFINAE导致编译器错误,它应该工作?
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()
)。我急于想知道这里出了什么问题。
这是什么编译器? –
如果'T'是'int *',那么'T *&'是'int **&'。所以我不认为签名匹配... – Nemo
@iammilind:不,它不应该匹配,你有'void foo(int **&)'和'int * p; foo(p);','p'是一个指向int的指针,而不是指向int的指针。 –