4

为什么这不起作用(Visual C++ 2012 Update 1),修复它的正确方法是什么?

#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);
}
4

2 回答 2

6

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;
于 2012-12-11T11:28:33.430 回答
3

一个编译器错误。

decltype -specifier (7.1.6.2 Simple type specifiers [dcl.type.simple]) 被明确地允许作为一个nested-name-specifier (5.1 Primary expressions [expr.prim] -> 5.1.1 General expr.prim.general] #8)

PS。在@ecatmur 的想法之后:

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

#define bindm(obj, f, ...)  bind(&id<decltype(obj)>::type::f, obj, __VA_ARGS__)
于 2012-12-11T10:59:49.510 回答