2013-01-14 51 views
2

下面的代碼snipplet試圖執行一個 '的std :: is_constructible < A,詮釋>':替補失敗有時是錯誤?

#include <type_traits> 

struct A { 
    // A(int); 
}; 

template< typename T > 
struct cstr_int 
{ 
    static int mi; 

    template< typename C > 
    static typename std::enable_if< 
    std::is_same< decltype(T(mi)), T >::value, char >::type choose(C *); 
    static long choose(...); 

    enum { value = (sizeof(decltype(choose(& mi))) == 1) }; 
}; 

bool const b1 = cstr_int<A>::value; 

使用克++這工作正常;使用鐺++以下錯誤被打印:

tmp/sfinae04.cc:14:29: error: no matching conversion for functional-style cast from 'int' to 'A' 
    std::is_same< decltype(T(mi)), T >::value, char >::type choose(C *); 
          ^~~~~ 
tmp/sfinae04.cc:20:17: note: in instantiation of template class 'cstr_int<A>' requested here 
bool const b1 = cstr_int<A>::value; 
       ^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'int' to 'const A' for 1st argument 
struct A { 
    ^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit move constructor) not viable: no known conversion from 'int' to 'A' for 1st argument 
struct A { 
    ^
tmp/sfinae04.cc:3:8: note: candidate constructor (the implicit default constructor) not viable: requires 0 arguments, but 1 was provided 
1 error generated. 

版本信息:鐺版本3.3(軀幹172418)(LLVM /中繼線172417)中,g ++(Debian的4.7.2-4)4.7.2。

我的問題: 恕我直言,這應該是一個SFINAE。我在這裏錯過了什麼,或者是否有與鏗鏘聲++的問題?

(我知道有is_constructible(),但我不能使用它 - 這是不是我的問題點。)

+2

我相信問題是,你在'enable_if'使用T和不是C。沒有替代失敗,因爲T = A是已知的。 –

+0

是的 - 你是對的:這也正是Bart van Ingen Schenau在他的回答中提到的。謝謝。 –

回答

6

儘管它可能看起來像一個替代故障給你,它實際上是在模板實例化過程中檢測到的故障,從而導致可診斷的錯誤。

它是而不是一個SFINAE上下文,因爲enable_if不依賴於推導的模板參數。

這個工程:

template< typename T > 
struct cstr_int 
{ 
    static int mi; 

    template< typename C > 
    static typename std::enable_if< 
    std::is_same< decltype(C(mi)), C >::value, char >::type choose(C *); 
    static long choose(...); 

    enum { value = (sizeof(decltype(choose((T*)0))) == 1) }; 
}; 
+0

Uups!非常感謝你的提示 - 我一定是盲目的... –