SNR_BT
SNR_BT

Reputation: 183

How to establish trailing return type of lambda function that use template functions?

How can I make trailing return type deduction in the following code snippet that uses a template function? Code works well as it is now, but I need to know if I can also add trailing return for the lambda function..

template<class T>
T print(T a){
    cout << a;
    return a;
};

int main()
{
    auto print_int = [](int a)/*->How?*/{
        return print<int>(a);
    };
    print_int(4);
}

Upvotes: 0

Views: 65

Answers (2)

cigien
cigien

Reputation: 60238

You can simply do:

auto print_int = [](int a) -> auto {
    return print<int>(a);
};

or

auto print_int = [](int a) -> decltype(auto) {
    return print<int>(a);
};

Upvotes: 0

NutCracker
NutCracker

Reputation: 12273

You can do the following:

auto print_int = [](int a) -> decltype(print<int>(a)) {
    return print<int>(a);
};

Upvotes: 4

Related Questions