4

我有这样的课:

class Test {
public:
    bool bar(int &i, char c) // some arguments are passed by ref, some are by value
    {/*...*/}
    bool foo(/*...*/)
    {}
};

而且我不想重复调用bar1/bar2等然后一次又一次地检查返回值,所以我写了一个宏和可变参数模板来处理那些事情

#define help_macro(object, memfn, ...) help_func(#object "." #memfn, \
        object, &decltype(object)::memfn, ##__VA_ARGS__)

template<class T, typename Func, typename... Args>
void help_func(char const * name, T &&object, Func memfn, Args&&... args)
{
    auto ret = (object.*memfn)(forward<Args>(args)...);
    cout<<name<<":\t"
        <<(ret ? "OK" : "Oops")  // maybe I'll throw an exception here  
        <<endl;
}

并像这样使用它

int i = 0;
Test t;
help_macro(t, bar, i, 'a');

它适用于 g++-4.7/Debian,但 ICC13.0/Win 拒绝编译它(一个非常奇怪的错误消息)

main.cpp(37): error : type name is not allowed
help_macro(t, bar, i, 'a');
^
main.cpp(37): 错误: 预期一个 ")"
help_macro(t, bar, i, 'a');
^

我为ICC打开了C++11,并确认ICC13支持可变参数模板和decltype是我使用不正确还是ICC的问题?

4

2 回答 2

2

编辑:实际上已经费心测试我的理论,结果证明我错了,在这种情况下decltype(t)Test可以通过static_assert(std::is_same<decltype(t), Test>::value, "not a reference")

因此 ICC(或它使用的 EDG 前端)可能只是不正确支持decltype在嵌套名称说明符中使用,这已由DR 743更改

使用std::decay 确实使 ICC 接受它,因此是一个有用的解决方法。

原文,错误,答案

我认为ICC就在这里,decltype(object)实际上Test&是引用类型不能有成员,所以&decltype(t)::memfn格式不正确。

代码可以简化为:

struct Test {
    void foo() {}
};

int main()
{
  Test t;
  auto p = &decltype(t)::foo;
}

哪个 G++ 和 Clang 接受,但 ICC 拒绝,恕我直言。

您可以使用std::remove_reference或修复它std::decay

#include <type_traits>

// ...

Test t;
auto p = &std::decay<decltype(t)>::type::foo;
于 2013-01-24T14:19:58.980 回答
0

I think the reason is there is no 'bar' function in class Test but not sure as I don't have access to this compiler. However the error message you posted shows there is an attempt to use 'bar'.

The following works on both gcc and clang

class Test {
public:
    bool bar1(int &i, char c) // some arguments are passed by ref, some are by value
    {return true;}
    bool bar2(int &i, char c)
    {return true;}
};

#define help_macro(object, memfn, ...) help_func(#object "." #memfn, \
        object, &decltype(object)::memfn, ##__VA_ARGS__)

template<class T, typename Func, typename... Args>
void help_func(char const * name, T &&object, Func memfn, Args&&... args)
{
    auto ret = (object.*memfn)(std::forward<Args>(args)...);
    std::cout<<name<<":\t"
        <<(ret ? "OK" : "Oops")  // maybe I'll throw an exception here
        <<std::endl;
}

int main()
{
    int i = 0;
    Test t;
    //help_macro(t, bar, i, 'a');
    help_macro(t, bar2, i, 'a');
}
于 2013-01-24T09:36:30.760 回答