【发布时间】:2016-06-24 20:23:23
【问题描述】:
我尝试使用以下技术根据类模板参数的属性有条件地制作 默认构造函数 = default;:
#include <type_traits>
#include <utility>
#include <iostream>
#include <cstdlib>
template< typename T >
struct identity
{
};
template< typename T >
struct has_property
: std::false_type
{
};
template< typename T >
struct S
{
template< typename X = T,
typename = std::enable_if_t< !has_property< X >::value > >
S(identity< X > = {})
{ std::cout << __PRETTY_FUNCTION__ << std::endl; }
template< typename X = T,
typename = std::enable_if_t< has_property< X >::value > >
#if 0
S() = default;
#else
S()
{ std::cout << __PRETTY_FUNCTION__ << std::endl; }
#endif
};
struct A {};
struct B {};
template<>
struct has_property< B >
: std::true_type
{
};
int main()
{
S< A >{};
S< B >{};
return EXIT_SUCCESS;
}
但是对于#if 1,它给出了一个错误:
main.cpp:32:11: error: only special member functions may be defaulted
S() = default;
^
template< ... > S() 不是 S 的 默认构造函数 的声明吗?
我可以在未来使用即将推出的概念来实现这样的调度吗?
【问题讨论】:
标签: c++ templates c++11 c++14 default-constructor