lundi 2 janvier 2017

Cannot deduce return type of lambda with std::map

This is a strange case.

  • The following code produces error when using User defined types but not when using primitives!
  • It produces errors with std::map but not if replaced with std::pair!

Why?

class Class {};

typedef double Type;   // This works fine
//typedef Class Type;  // This produces error

template<typename T, typename ValueFunctor>
std::map<int, typename std::result_of<ValueFunctor(T)>::type >
testFun(ValueFunctor valueFunctor)
{
    std::map<int, typename std::result_of<ValueFunctor(T)>::type > map;

    return map;
}


void test()
{
    std::map<int,Type> output = testFun<Type>(
        // The error can be removed by explicitly specifying return type
        [](const Type &pair)/*->T2*/{
            return pair;
        }
    );

}

Aucun commentaire:

Enregistrer un commentaire