Partialization based on argument features

Suppose I have the following template:

template <typename T> union example {
    T t;

    constexpr example(const T & t) : t(t) {};

    /* We rely on owning class to take care
     * of destructing the active member */
    ~example() {};
};

Because of the destructor, there example<T>will never be trivially destructible (and therefore will not be, say, a literal type). I would like to have a partial specialization, for example

template <typename T> union
    example<std::enable_if_t<std::is_trivially_destructible<T>::value, T>> {
    T t;

    constexpr example(const T & t) : t(t) {};
};

so that example<T>it is trivially destructible when Tthere is, but, unfortunately, it gives me a (reasonable, retroactively) warning

warning: classification of a template template contains a template parameter that cannot be displayed; this partial specialization will never be used

So, is there a way to get what I want here?

+4
source share
1

, ?

#include <type_traits>
#include <iostream>

template <typename T, bool = std::is_trivially_destructible<T>::value>
union example {
    T t;

    constexpr example(const T & t) : t(t) {};

    /* We rely on owning class to take care
     * of destructing the active member */
    ~example() { std::cout << "primary template\n"; }
};

template<typename T>
union example<T, true> {
    T t;

    constexpr example(const T & t) : t(t) {};
};


struct nontrivial
{
    ~nontrivial() { std::cout << "woot!\n"; }
};

int main()
{
    example<nontrivial> e1{{}};
    example<int> e2{{}};
}
+6

Source: https://habr.com/ru/post/1530513/


All Articles