7
#include <functional>
#include <iostream>
template<typename T>
class MaybePtr{
    T* ptr;

public:
    MaybePtr(T* p) : ptr(p) {}

    template <typename F,typename R = std::result_of<F(T*)>::type>
    R Get(F access,F default){
        if (ptr != nullptr)
            return access(ptr);
        else
            return default(ptr);   
    }
};
template <typename T>
void f_void(T*) {}
int main(){
    int * iptr = new int;
    *iptr = 10;
    auto m = MaybePtr<int>(iptr);
    auto f = [](int* i) -> int {return *i + 1; };
    auto f1 = [](int* i) -> int { return 0; };
    int r = m.Get(f, f1); // error C2782
    std::cout << f(iptr);
    int i;
    std::cin >> i;
}

Error

error C2782: 'R MaybePtr<int>::Get(F,F)' : template parameter 'F' is ambiguous

Why is F ambiguous? It should know that F is a function that takes a T* and returns a R.

4
  • The type R depends on the type T, and then you want the type T to be derived from the type R? Jul 28, 2014 at 11:09
  • 3
    Note: g++ is a little bit more clear: deduced conflicting types for parameter ‘F’ (‘main()::__lambda0’ and ‘main()::__lambda1’) // error C2782
    – Zeta
    Jul 28, 2014 at 11:14
  • @Zeta - Yes. g++ template error messages become much clearer.
    – SChepurin
    Jul 28, 2014 at 12:21
  • 2
    BTW, you may avoid identifier default.
    – Jarod42
    Jul 28, 2014 at 12:42

1 Answer 1

12

A lambda with an empty closure may decay to a function pointer but here :

static_assert( std::is_same<decltype(f),decltype(f1)>::value,"different types" );

The error is normal, also Visual Studio is laxist, but you miss a typename and default is a reserved keyword.

template <typename F,typename R = typename std::result_of<F(T*)>::type>

A trick exists: force the lambda to decay to a function pointer. The below line compiles and does what you expect, of course, only valid with empty closure :

int r = m.Get(+f, +f1);
9
  • 1
    @MarcoA.: Had the same idea. Drawback: You loose the brevity given by auto.
    – Zeta
    Jul 28, 2014 at 11:17
  • And it introduces std::function, a heavy object, adding indirection, possible copy, …
    – galop1n
    Jul 28, 2014 at 11:18
  • 1
    @galop1n Can you describe a bit more that +f ? I do not understand what it is and what it does.
    – Johan
    Jul 28, 2014 at 11:20
  • 2
    @Johan It basically tries to apply the + operator to something that can be only operated in this context via a pointer, as a stateless lambda can decay to a function pointer it does that. "5.1.2.6 The closure type for a non-generic lambda-expression with no lambda-capture has a public non-virtual non- explicit const conversion function to pointer to function (...)"
    – Red XIII
    Jul 28, 2014 at 12:01
  • 1
    Just for the reference: "The operand of the unary + operator shall have arithmetic, unscoped enumeration, or pointer type and the result is the value of the argument." That's important as other unary operators don't have this property.
    – Red XIII
    Jul 28, 2014 at 13:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.