Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22118

How to make traits to accept a parameter pack?

$
0
0

I define some type traits like this:

template <typename T>struct has_something{    static constexpr bool value = false;};template <> struct has_something<int>{    static constexpr bool value = true;};template <typename T>constexpr bool has_something_v = has_something<T>::value;

And a function template which is has_something_v is a requirement for function parameter:

template <typename T, typename = std::enable_if_t<has_something_v<T>>>void some_function(const T temp){}

When i call it with wrong type:

struct wrong_type{};void f (){    some_function(wrong_type());}

compiler give me a proper error message:

/tmp/untitled/main.cpp:23: candidate template ignored: requirement 'has_something_v<wrong_type>' was not satisfied [with T = wrong_type]

but when i called with another template function:

template <typename ...T, typename = std::enable_if_t<has_something_v<T...>>>void some_function(const T... args){    (some_function(args), ...);}void f (){    some_function(1, 2, a());}

compiler give me really bad and confusing error message because i don't have a parameter pack acceptable traits :

Compiler error message

And if i remove std::enable_if from last template function, everything work fine until i send a wrong_type type to function which result is in crashing program.

For parameter pack, i wrote this:

template <typename ...T>struct has_something{    static bool value;    static constexpr bool c(T... args)    {        value = (args && ...);        return value;    }};template <>struct has_something<int>{    static constexpr bool value = true;};template <typename ...T>const bool has_something_v = has_something<T...>::value;

But it still fail.

How could i write a acceptable parameter pack type traits ?


Viewing all articles
Browse latest Browse all 22118


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>