2012-05-31 32 views
10

梗概

我正在與製備C++ 11代碼鏘兼容掙扎跑進其中GCC> = 4.6接受代碼和鏘> = 3.1的情況下則沒有。鏗鏘認爲candidate constructor not viable鏘問題:在構造時隱式類型轉換

詳細

下面是一個下調的例子來說明這個問題:

#include <utility> 

template <typename...> 
struct T; 

template<> 
struct T<> 
{ 
    typedef T super; 

    constexpr T() { } 

    template <typename... Args> 
    T(Args&&...) { } 

}; 

template <typename Head, typename... Tail> 
struct T<Head, Tail...> : T<Tail...> 
{ 
    typedef T<Tail...> super; 

    Head head; 

    T(Head arg) : super(), head(std::move(arg)) { } 
}; 


struct void_type 
{ 
    constexpr void_type() { } 
    constexpr void_type(const void_type&) { } 
    void_type& operator=(const void_type&) = default; 

    template <typename Arg0, typename... Args> 
    void_type(Arg0&&, Args&&...) { } 
}; 

struct atom { }; 

int main() 
{ 
    atom a; 
    T<void_type> t(a); 

    return 0; 
} 

我得到的錯誤是:

ctor-init.cpp:44:18: error: no matching constructor for initialization of 'T<void_type>' 
    T<void_type> t(a); 
       ^~ 
ctor-init.cpp:19:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'atom' to 'const T<void_type>' for 1st argument; 
struct T<Head, Tail...> : T<Tail...> 
    ^
ctor-init.cpp:25:5: note: candidate constructor not viable: no known conversion from 'atom' to 'void_type' for 1st argument; 
    T(Head arg) : super(), head(std::move(arg)) { } 
    ^
1 error generated. 

我不明白爲什麼鐺抱怨缺乏的轉換可能性,因爲我認爲這個「通吃」的構造函數應該工作:

template <typename Arg0, typename... Args> 
void_type(Arg0&&, Args&&...) { } 

所以我很困惑的錯誤是:

ctor-init.cpp:25:5: note: candidate constructor not viable: no known conversion from 'atom' to 'void_type' for 1st argument; 
    T(Head arg) : super(), head(std::move(arg)) { } 
    ^

畢竟,GCC接受的代碼。這可能是一個詛咒錯誤? (我正在使用LLVM git倉庫中的最新Clang。)

+0

@dirkgently:然而,即使使用這個名字,你也應該能夠提供幫助。畢竟你是找到薛定諤貓的偵探。 – LiKao

+1

如果您嘗試從原子直接創建void_type,會發生什麼情況:'void_type v(a)'? –

+0

@DaveS:這個編譯就好了。它給你一個線索爲什麼構造函數中的隱式轉換失敗? – mavam

回答

1

事實上,這是一個叮噹bug。事實證明,可變參數構造函數被錯誤地標記爲明確的。 Fixed in Clang r158040