c++ - &decltype(object)::memfn 是误用吗?

标签 c++ c++11 decltype

我上过这样的课:

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): error : expected a ")"
help_macro(t, bar, i, 'a');
^

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

最佳答案

编辑:实际上费心去检验我的理论,结果证明我错了,在那种情况下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;

关于c++ - &decltype(object)::memfn 是误用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497371/

相关文章:

c++ - 如何在没有多态效果的情况下调用虚方法?

c++ - 将 node-ffi 与 Electron 一起使用无法正常工作(无法找到消息编号 0x%1 的消息文本...)

C++ - vector <class> 和 std::bad_alloc

c++ - 在 C++11 中,您能否始终(安全地)用空初始化器替换 memset() 初始化?

c++ - 使用 auto 和 decltype 使函数返回其类的类型。我怎样才能让它返回一个值,而不是一个引用?

c++ - 有没有办法在变量中存储类型说明符?

c++ - 执行 decltype(c) e;和 decltype((c)) f;声明不同的类型?

c++ - 使用 Cobertura 在 Jenkins 中显示源代码的代码覆盖率(其他机器的运行结果)

C++ 多级模板化

c++ - 为什么这个可变函数不明确?