0

Context: Trying to create a class to unpack a tuple (used to store variadic template arguments). Inspired by this answer https://stackoverflow.com/a/24804339/11510510 I have something that does the job.

(I'm restricted to c++11 if not I would be using std::apply from C++17)

Problem: It only compiles when the object is created by using the specific type and not inside a template.

Target class to instantiate

    template<class C, class ...Args>
    struct APPLY_TUPLE{};
    
    template<class C, class ...Args>
    struct APPLY_TUPLE<C, std::tuple<Args...>>{
        // irrelevant code
    };

Class Instantiating the APPLY_TUPPLE

    template<typename C, typename R>
    struct TupledCall
    {     
        TupledCall(C& obj) : object{obj} {}
     
        R MethodTrigger(){
             // instantiate APPLY_TUPLE here with type of object        
        }

        C &object;
    };

/*Instantiation as*/
auto obj = Summator();
auto tupledCall = TupledCall<Summator, int>(obj);
tupledCall.MethodTrigger();

What compiles

R MethodTrigger(){
    tuple<int,int,int> list = make_tuple(1,5,4);

    auto tup = APPLY_TUPLE<Summator, args(list)>(object);
    return tup.delayed_call<int , &Summator::sum>(list);
}

R MethodTrigger(){
    tuple<int,int,int> list = make_tuple(1,5,4);
    
    Summator s = Summator();
    auto tup = APPLY_TUPLE<typename decay<decltype(s)>::type, args(list)>(object);
    return tup.delayed_call<int , &Summator::sum>(list);
}

What doesn't compile

R MethodTrigger(){
    tuple<int,int,int> list = make_tuple(1,5,4);

    auto tup = APPLY_TUPLE<C, args(list)>(object);
    return tup.delayed_call<int , &Summator::sum>(list);
}

R MethodTrigger(){
    tuple<int,int,int> list = make_tuple(1,5,4);
    
    auto tup = APPLY_TUPLE<typename decay<decltype(object)>::type, args(list)>(object);
    return tup.delayed_call<int , &Summator::sum>(list);
}

R MethodTrigger(){
    tuple<int,int,int> list = make_tuple(1,5,4);
    
    C sum = C(); // C should be deducted as Summator in this template
    auto tup = APPLY_TUPLE<typename decay<decltype(object)>::type, args(list)>(object);
    return tup.delayed_call<int , &Sumator::sum>(list);
}

What is the difference between them? Why it doesn't compile when trying to make the solution generic?

Compiler error

    <source>: In member function 'R TupledCall<C, R>::MethodTrigger()':
<source>:80:37: error: expected primary-expression before 'int'
   80 |             return tup.delayed_call<int , &Summator::sum>(list);
      |                                     ^~~
<source>:80:37: error: expected ';' before 'int'
<source>:80:41: error: expected unqualified-id before ',' token
   80 |             return tup.delayed_call<int , &Summator::sum>(list);
      |                                         ^
<source>:80:57: error: qualified-id in declaration before '>' token
   80 |             return tup.delayed_call<int , &Summator::sum>(list);

Minimal reproducible example https://godbolt.org/z/bPYWhfM7d

MrCas
  • 51
  • 5
  • 4
    A [mcve] is needed here. `auto tup = APPLY_TUPLE(object);` uses `args` which is defined nowhere? – Yakk - Adam Nevraumont Jul 17 '21 at 13:29
  • 4
    But it might you need a `.template delayed_call` – Yakk - Adam Nevraumont Jul 17 '21 at 13:32
  • 1
    Please post a [mcve]. – n. m. could be an AI Jul 17 '21 at 13:36
  • Thank you for the answers! I updated the post with a minimal reproducible example. As pointed out by @Yakk-AdamNevraumont I need to add `.template` before the call to delayed call. – MrCas Jul 18 '21 at 07:59
  • And for completely understanding the issue, since `delayed_call` is anyway a template, what is the difference between explicitly indicating the type `Summator` and `C` without the `.template delayed_call` ? – MrCas Jul 18 '21 at 08:05
  • The M in mcve is mi imal. `args` and 3 other macros all do the same thing. Discard and expand macros, they are noise in finding the cause of the error. One of your problems is using a dependent template without didsmbuator, but the macro use in the mcve make that annoying to track down. – Yakk - Adam Nevraumont Jul 18 '21 at 09:53
  • Sorry. I didn't want to be one of those people who give a bunch of code so that's why I didn't do it in the first place. I'm a bit out of my confort zone with this topic and I didn't know how to minimize more the mcve. Anyway I appreciate a lot your time and your help. I've applied your tips with the macros. Thank you very much Adam. – MrCas Jul 18 '21 at 19:25

0 Answers0