Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++14 auto lambda can accept Obj<std::tuple<void> > -- but template functions cannot?

Below is a program that completely demonstrates the problem I'm seeing.

First, I start with an object that is defined using a grouping of other types, I started using a std::tuple<> to manage the grouping.

template <typename> class object;
template <typename... Rs> class object<std::tuple<Rs...> > {
};

I am intending these objects to be capable of having the type void scattered in the "pack". I am already aware of being unable to "instantiate" a tuple of this type (see Void type in std::tuple)

I want to pass these objects around, perhaps copy/move them... none of their data members are a tuple of these types. In fact, I can reproduce the problem using the empty object definition above.

I can make it work, using something like:

template <typename... Rs> struct TGrp {};

template <typename> class object;
template <typename... Rs> class object<TGrp<Rs...> > {
};

These types of "grouping" structs are used frequenly in variadic recursion, and they are meant to never get created/used. Just to group template args.

However, I "want" the signature of the 'object' to be made up of "user expected" types/names.

Basically, I was experimenting with any possible way of passing one of these objects around when std::tuple is used to "group", and could only find one way: auto lambdas.

Can anybody explain:

  1. why the "auto" lambda's can work for this?

    something about delayed template deduction? like the diff b/w "auto" and "decltype(auto)"?

  2. how to "design" a function parameter to accept one of these objects.

-- thanks to you all for any insights on this oddity

Example:

#include <tuple>
#include <iostream>

#define GRP std::tuple      // IF 'tuple' used:  compile error where noted below
//#define GRP TGrp          // if THIS is used:  all works, and TGrp() is never constructed


// Grouping mechanism
template <typename... Ts> struct TGrp {
    TGrp() {
        std::cout << "Never printed message\n";
    }
};


// MAIN OBJECT (empty for forum question)
template <typename> class object;
template <typename... Rs> class object<GRP<Rs...> > {
};



// Regular function                  (does NOT work)
void takeobj(object<GRP<void> >& obj) { (void)obj; }

// Lambda - taking anything...       (only thing I could make WORK)
auto takeobj_lambda = [](auto obj) { (void)obj; };

// Template func - taking anything   (does NOT work)
template <typename T> void takeobj_templ_norm(T obj) { (void)obj; }
template <typename T> void takeobj_templ_clref(const T& obj) { (void)obj; }
template <typename T> void takeobj_templ_lref(T& obj) { (void)obj; }
template <typename T> void takeobj_templ_rref(T&& obj) { (void)obj; }


int main()
{
    object<GRP<void> > oval;

    //takeobj(oval);                  // <--    causes compile error

    takeobj_lambda(oval); // works

    //takeobj_templ_norm(oval);       // <--    also error
    //takeobj_templ_clref(oval);      // <--    also error
    //takeobj_templ_lref(oval);       // <--    also error
    //takeobj_templ_rref(oval);       // <--    also error
    return 0;
}

Edit: adding a trimmed down reproduction:

#include <tuple>


// MAIN OBJECT (empty for forum question)
template <typename> class object;
template <typename... Rs> class object<std::tuple<Rs...> > {
};

// Regular function                  (does NOT work)
void takeobj(object<std::tuple<void> >& obj) { (void)obj; }

// Lambda - taking anything...       (only thing I could make WORK)
auto takeobj_lambda = [](auto obj) { (void)obj; };


int main()
{
    object<std::tuple<void> > oval;

    //takeobj(oval);                  // <--    causes compile error
    takeobj_lambda(oval); // works

    return 0;
}
like image 566
CrashNeb Avatar asked Jan 27 '16 03:01

CrashNeb


People also ask

Can lambda function be a template?

From the various lambda improvements, template parameters for lambdas are my favorite ones. Lambdas support with C++20 template parameters, can be default-constructed and support copy-assignment, when they have no state, and can be used in unevaluated contexts.

Can you template lambda C++?

C++0x lambdas are monomorphic and can't be templated, unfortunately.

How do you declare a lambda function in C++?

Creating a Lambda Expression in C++auto greet = []() { // lambda function body }; Here, [] is called the lambda introducer which denotes the start of the lambda expression. () is called the parameter list which is similar to the () operator of a normal function.

What is the type of lambda function in C++?

A lambda is an object (hence why we're referring to it as a functor, rather than a function) so has a type and can be stored. However, the type of the lambda is only known by the compiler (since it is compiler-generated), so you must use auto for declaration instances of the lambda.


1 Answers

std::tuple<void> is an associated class of object<std::tuple<void>>, and so in an unqualified call in which argument-dependent lookup is performed, std::tuple<void> is instantiated to look for any friend functions that might have been defined inline. This instantiation causes an error.

Argument-dependent lookup is not performed if the thing being called doesn't name a function or function template; hence using a lambda works - takeobj_lambda is an object.

If you use either a qualified call (::takeobj(oval)), or parenthesize takeobj ((takeobj)(oval)), then the code compiles. Both of these disable ADL.

like image 101
T.C. Avatar answered Sep 30 '22 02:09

T.C.