Search code examples
c++variadic-templatestemplate-meta-programmingsfinaeparameter-pack

Get member from last possible class of a parameter pack


I want to get the value of a member variable of the last possible class in a parameter pack.

For eg. I want getLastB(a_, b_, c_, d_) to return 100 in the code below (the value of c_.b), and not 40 (the value of b_.b)

#include <type_traits>
#include <iostream>

#define ADD_HAS_MEM_VAR_CHECKER(var, name)                              \
    template<typename T>                                                \
    struct name {                                                       \
        typedef char yes[1];                                            \
        typedef char no [2];                                            \
        template <typename _1> static yes &chk(decltype(_1::b));        \
        template <typename   > static no  &chk(...);                    \
        static bool const value = sizeof(chk<T>(0)) == sizeof(yes);     \
    }


ADD_HAS_MEM_VAR_CHECKER(b, has_b);

template<typename Arg, typename... Args>
struct has_b_parampack {
    static bool const value = has_b<Arg>::value or has_b_parampack<Args...>::value;
};

template<typename Arg>
struct has_b_parampack<Arg> {
    static bool const value = has_b<Arg>::value;
};


template <typename Arg, typename... Args>
typename std::enable_if<has_b_parampack<Args...>::value, int>::type
getLastB(Arg first, Args&&... args) {
    return getLastB(args...);
}

template <typename Arg, typename... Args>
typename std::enable_if<!has_b_parampack<Args...>::value, int>::type
getLastB(Arg first, Args&&... args) {
    return first.b;
}


struct A {int a = 10;};
struct B {int b = 40;};
struct C {int c = 30;int b = 100;};
struct D {int d = 30;};


int main() {
    A a_;
    B b_;
    C c_;
    D d_;

    std::cout << "has B parampack A, B, C, D= " << has_b_parampack<A, B, C, D>::value << std::endl;
    std::cout << "has B parampack B, C, D= " << has_b_parampack<B, C, D>::value << std::endl;
    std::cout << "has B parampack C, D= " << has_b_parampack<C, D>::value << std::endl;
    std::cout << "has B parampack D= " << has_b_parampack<D>::value << std::endl;

    std::cout << "Last B = " << getLastB(a_, b_, c_, d_) << "\n";
    return 0;
}

However when I try to compile this code, I get an error

lastMemberOfParamPack.cpp: In instantiation of ‘typename std::enable_if<(! has_b_parampack<Args ...>::value), int>::type getLastB(Arg, Args&& ...) [with Arg = A; Args = {B&, C&, D&}; typename std::enable_if<(! has_b_parampack<Args ...>::value), int>::type = int]’:
lastMemberOfParamPack.cpp:58:56:   required from here
lastMemberOfParamPack.cpp:37:18: error: ‘struct A’ has no member named ‘b’
     return first.b;

If I comment the line where I call getLastB from main, the code compiles and it does give the expected values for the 4 print statements.

has B parampack A, B, C, D= 1
has B parampack B, C, D= 1
has B parampack C, D= 1
has B parampack D= 0

Any ideas what I might be doing wrong here?


Solution

  • You need to remove reference from T

    static bool const value = sizeof(chk<typename std::remove_reference<T>::type>(0)) == sizeof(yes);
    

    Your code would also fail for situations like when the last one contains b, or b is not of type int.


    If you can use C++17 you can write it like this:

    template<typename T>                                               
    struct has_b {                                                     
        typedef char yes[1];
        typedef char no [2];
        template <typename S> static yes& chk(decltype(S::b));
        template <typename  > static no & chk(...);
        static bool const value = sizeof(chk<typename std::remove_reference<T>::type>(0)) == sizeof(yes);
    };
    
    template <typename Arg, typename... Args>
    auto getLastB(Arg& first, Args&... args) {
        if constexpr((has_b<Args>::value or...))
            return getLastB(args...);
        else
            return first.b;
    }