使用`void_t`来检测多个继承类型的重复错误

时间:2015-08-14 18:57:34

标签: c++ templates sfinae c++17 variadic-functions

如果传递的可变参数类型列表没有重复类型,我想实现一个has_no_duplicates<...>类型特征,其值为std::true_type

static_assert(has_no_duplicates<int, float>{}, "");
static_assert(!has_no_duplicates<float, float>{}, "");

对于这个问题的范围,我们假设我想使用多重继承来做到这一点。

当一个类多次从同一类型继承时,会发生错误。

template<class T> 
struct type { };

template<class... Ts>
struct dup_helper : type<Ts>... { };

// No errors, compiles properly.
dup_helper<int, float> ok{};

// Compile-time error: 
// base class 'type<float>' specified more than once as a direct base class
dup_helper<float, float> error{};

我认为我可以使用void_t来检测&#34;&#34;这个错误,但我无法实施有效的解决方案following the code samples from cppreference

这就是我的尝试:

template<class, class = void>
struct is_valid 
    : std::false_type { };

// First try:
template<class T>
struct is_valid<T, std::void_t<decltype(T{})>> 
    : std::true_type { };

// Second try:
template<class T>
struct is_valid<T, std::void_t<T>> 
    : std::true_type { };

对于我的第三次尝试,我尝试使用包含dup_helper<...>作为模板模板参数的包装类来延迟dup_helper的扩展,如wrapper<dup_helper, ...>并在{{1}内扩展它}}

不幸的是,我的所有尝试都导致上述错误总是阻止编译。

我认为此类错误无法被检测为&#34;替换失败&#34;,但我想要确认。

使用void_t实际上无法检测到这种错误吗? (它总会导致编译失败吗?)

有没有办法检测它而不会导致编译失败? (或者非void_t解决方法仍然使用&#34;多重继承技巧&#34;)?

2 个答案:

答案 0 :(得分:6)

正如@Canoninos所说,问题在于:

  

dup_helper<T, T>的声明不是导致错误的声明,而是其定义[...]。

或者,在Standardese中,错误发生在替换的“直接上下文”( [temp.deduct] )之外:

  

8 - [...]只有函数类型的直接上下文中的无效类型和表达式   其模板参数类型可能导致扣减失败。 [注意:对替代类型的评估   和表达式可能会导致副作用,例如实例化类模板特化和/或   函数模板特化,隐式定义函数的生成等。这样的副作用是   不在“直接背景”中,可能导致程序格式不正确。 - 结束记录]

在实例化 dup_helper<float, float>时,错误发生,因此不在“直接上下文”中。

一个非常接近你的多重继承技巧涉及通过索引多个基数来添加额外的继承层:

helper<<0, 1>, <float, float>>        
             +            
        +----+----+       
        v         v       
 ix<0, float>  ix<1, float>
        +         +       
        v         v       
     t<float>  t<float>   

这为我们提供了一个带有有效定义的辅助类,并且可以实例化但不会强制转换为它的最终基类,因为含糊不清:

static_cast<t<float>>(helper<...>{});  // Error, SFINAE-usable

Example

答案 1 :(得分:0)

这是我使用元编程和类型列表习语的解决方案。 我将此代码用作实现C ++反射的库的一部分。我认为在void_t或继承中根本不需要解决这个任务。

template <typename ...Args>
struct type_list
{};

using empty_list = type_list<>;

// identity
template<typename T>
struct identity
{
    using type = T;
};

// is_typelist
template<typename T>
struct is_typelist: std::false_type
{};

template<typename ...Args>
struct is_typelist<type_list<Args...>>: std::true_type
{};

template<typename T>
struct check_typelist
{
    using type = void;
    static constexpr void *value = nullptr;
    static_assert(is_typelist<T>::value, "T is not a type_list!");
};

// indexof
namespace internal {

template<typename T, typename V, std::int64_t index>
struct typelist_indexof_helper: check_typelist<T>
{};

template<typename H, typename ...T, typename V, std::int64_t index>
struct typelist_indexof_helper<type_list<H, T...>, V, index>:
        std::conditional<std::is_same<H, V>::value,
            std::integral_constant<std::int64_t, index>,
            typelist_indexof_helper<type_list<T...>, V, index + 1>
        >::type
{};

template<typename V, std::int64_t index>
struct typelist_indexof_helper<empty_list, V, index>: std::integral_constant<std::int64_t, -1>
{};

}

template<typename T, typename V>
using typelist_indexof = ::internal::typelist_indexof_helper<T, V, 0>;

template<typename T, typename V>
struct typelist_exists: std::integral_constant<bool, typelist_indexof<T, V>::value >= 0>
{};

// remove_duplicates
namespace internal {

template<typename P, typename T>
struct typelist_remove_duplicates_helper: check_typelist<T>
{};

template<typename ...P, typename H, typename ...T>
struct typelist_remove_duplicates_helper<type_list<P...>, type_list<H, T...>>:
        std::conditional<typelist_exists<type_list<T...>, H>::value,
            typelist_remove_duplicates_helper<type_list<P...>, type_list<T...>>,
            typelist_remove_duplicates_helper<type_list<P..., H>, type_list<T...>>
        >::type
{};

template<typename ...P>
struct typelist_remove_duplicates_helper<type_list<P...>, empty_list>: identity<type_list<P...>>
{};

}

template<typename T>
using typelist_remove_duplicates = ::internal::typelist_remove_duplicates_helper<empty_list, T>;


template<typename ...Args>
struct has_no_duplicates: std::integral_constant<bool, std::is_same<type_list<Args...>,
                                                                    typename typelist_remove_duplicates<type_list<Args...>>::type>::value>
{};

DEMO