C C
C C

Reputation: 3

C++ how to check for all variadic template type in a member function

e.g. I have the following Foo class with the foo() to check if all the types are std::int32_t

class Foo {
public:
    /// check if all the types are std::int32_t
    template<typename ...Ts>
    bool foo() {
        return true && std::is_same<Ts, std::int32_t>::value...;
    } 
};

int main()
{
    Foo f;
    std::cout<<f.template foo<std::int32_t, std::int32_t>(); //true
    std::cout<<f.template foo<std::int32_t, std::int64_t>(); //false

    return 0;
}

return true && std::is_same<Ts, std::int32_t>::value...; is not a correct syntax. How do I make it correct?

Upvotes: 0

Views: 293

Answers (1)

KamilCuk
KamilCuk

Reputation: 141748

https://en.cppreference.com/w/cpp/language/fold

return (true && ... && std::is_same<Ts, std::int32_t>::value);
# or
return (std::is_same<Ts, std::int32_t>::value && ... && true);
# or really just
return (std::is_same<Ts, std::int32_t>::value && ...);

Upvotes: 2

Related Questions