我試圖實現一個元程序,它發現給定的指針類型是否爲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的指針。 –