【发布时间】:2011-07-05 17:17:33
【问题描述】:
我试图实现一个元程序,它会发现给定的指针类型是否为const。即
-
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 **&。所以我认为签名不匹配...... -
@iammilind:不,应该不匹配,你有
void foo( int **& )和int *p; foo( p );,p是指向int的指针,而不是指向int的指针。
标签: c++ templates compiler-errors sfinae