我有这样的课:
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的问题?