相关疑难解决方法(0)

使用具有作用于函数内部的匿名类的模板

假设我有以下代码段:

template <class T> void f(T arg) { arg(); }

void g()
{
   struct { void operator()(void) { } } foo;

   f(foo);
}
Run Code Online (Sandbox Code Playgroud)

Visual C++接受这个.但是,当我尝试GCC时,我得到:

$ g++ --version # just in case this matters
g++ (Debian 4.4.5-8) 4.4.5
...
$ g++ foo.cc
foo.cc: In function 'void g()':
foo.cc:7: error: no matching function for call to 'f(g()::<anonymous struct>&)'
Run Code Online (Sandbox Code Playgroud)

foo全局作用域并且其类型具有名称时,这是有效的.但是当类型是匿名的在内部声明时g()它不会.

为什么GCC拒绝这个?它是有效的C++吗?

c++ templates anonymous-class

4
推荐指数
1
解决办法
1326
查看次数

标签 统计

anonymous-class ×1

c++ ×1

templates ×1