2016-11-26 44 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

相关问题