2016-11-26 85 views
3

當我使用SFINAE檢測模板類型是否爲默認可構造時,我剛剛觀察到libC++的一個奇怪問題。SFINAE與std :: enable_if和std :: is_default_constructible在libC++中的不完整類型

下面是一個小例子,我能夠想出:

#include <iostream> 
#include <type_traits> 

template <typename T> 
struct Dummy; 

template <> 
struct Dummy<int>{}; 

template <typename T, typename = void> 
struct has_dummy : std::false_type {}; 

template <typename T> 
struct has_dummy<C, std::enable_if_t<std::is_default_constructible<Dummy<T>>::value>> : std::true_type{}; 

int main() { 
    std::cout << std::boolalpha << has_dummy<int>{}() << '\n'; 
    std::cout << std::boolalpha << has_dummy<double>{}() << '\n'; 
} 

它編譯和預期線truefalse使用libstdc++時當與克++或鐺++編譯輸出。然而,當我試圖用的libc編譯它++(即clang++ -stdlib=libc++ -std=c++1z test.cpp)我得到以下錯誤:

/usr/bin/../include/c++/v1/type_traits:2857:38: error: implicit instantiation of undefined template 'Dummy' : public integral_constant

/usr/bin/../include/c++/v1/type_traits:3166:14: note: in instantiation of template class 'std::__1::is_constructible>' requested here : public is_constructible<_Tp>

test.cpp:14:43: note: in instantiation of template class 'std::__1::is_default_constructible >' requested here struct has_dummy<T, std::enable_if_t<std::is_default_constructible<Dummy<T>>::value>> : std::true_type{};

test.cpp:18:35: note: during template argument deduction for class template partial specialization 'has_dummy<type-parameter-0-0, typename enable_if<std::is_default_constructible<Dummy<T> >::value, void>::type>' [with T = double]

std::cout << std::boolalpha << has_dummy<double>{}() << '\n'; 

test.cpp:5:8: note: template is declared here struct Dummy;

這是在libc中++的實施std::enable_ifstd::is_default_constructible或錯誤是我在做某種方式調用特殊的未定義/執行什麼行爲?

最佳 Corristo

回答

5

前提條件is_default_constructible狀態相當清楚:

N4140 § 20.10.4.3 [meta.unary.prop]/is_default_constructible row

T shall be a complete type, (possibly cv-qualified) void , or an array of unknown bound.

而按照以下程序具有不確定的行爲:

N4140 § 17.6.4.8 [res.on.functions]/2

the effects are undefined in the following cases:

  • [...]
  • if an incomplete type (3.9) is used as a template argument when instantiating a template component, unless specifically allowed for that component.
+0

在圖書館,突破的先決條件結果在未定義的行爲中,除非另有說明。 –

+0

@ T.C。好吧,我似乎幾乎每天都從你那裏學到一些新東西:)現在好嗎?我不確定res.on.functions或res.on.required是否適用。後者有更好的名字,但明確提及功能,特徵不是功能,除非他們使用不同的功能 – krzaq

相關問題