我上过这样的课:
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/