Pregunta

Why is does this not work (Visual C++ 2012 Update 1), and what is the proper way to fix it?

#include <boost/lambda/bind.hpp>
namespace bll = boost::lambda;

struct Adder
{
    int m;
    Adder(int m = 0) : m(m) { }
    int foo(int n) const { return m + n; }
};

#define bindm(obj, f, ...)  bind(&decltype(obj)::f, obj, __VA_ARGS__)

int main()
{
    return bll::bindm(Adder(5), foo, bll::_1)(5);
}
¿Fue útil?

Solución

decltype as a nested-name-specifier was added into C++11 at a relatively late stage; n3049 as the resolution to DR 743 (and DR 950). n3049 was published in March 2010, which is probably why it hasn't found its way into VC++ yet.

The workaround is to use the identity typefunction:

template<typename T> using id = T;
id<decltype(expression)>::member;

Otros consejos

A compiler bug.

A decltype-specifier (7.1.6.2 Simple type specifiers [dcl.type.simple]) is explicitly allowed as a nested-name-speficier (5.1 Primary expressions [expr.prim] -> 5.1.1 General expr.prim.general] #8)

PS. After the @ecatmur's idea:

template<typename T> struct id { typedef T type; };

#define bindm(obj, f, ...)  bind(&id<decltype(obj)>::type::f, obj, __VA_ARGS__)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top