Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading