Gui*_*ira 7 c++ ambiguity forward-declaration object-construction
Observation: the codes pasted below were tested only with GCC 4.4.1, and I'm only interested in them working with GCC.
你好,
我偶然发现了一些我不理解的对象构造语句,而且直到今天我才注意到它引入的模糊性.我将解释如何重现它,并想知道是否有办法解决它(允许C++ 0x).在这里.
假设有一个类,其构造函数只接受一个参数,而这个参数的类型是另一个具有默认构造函数的类.例如:
struct ArgType {};
class Class
{
public:
Class(ArgType arg);
};
Run Code Online (Sandbox Code Playgroud)
如果我尝试Class在堆栈上构造一个类型的对象,我会产生歧义:
Class c(ArgType()); // is this an object construction or a forward declaration
// of a function "c" returning `Class` and taking a pointer
// to a function returning `ArgType` and taking no arguments
// as argument? (oh yeh, loli haets awkward syntax in teh
// saucecode)
Run Code Online (Sandbox Code Playgroud)
我说这是一个对象构造,但编译器坚持认为它是函数体内的前向声明.对于仍然没有得到它的人来说,这是一个完全有效的例子:
#include <iostream>
struct ArgType {};
struct Class {};
ArgType func()
{
std::cout << "func()\n";
return ArgType();
}
int main()
{
Class c(ArgType());
c(func); // prints "func()\n"
}
Class c(ArgType funcPtr()) // Class c(ArgType (*funcPtr)()) also works
{
funcPtr();
return Class();
}
Run Code Online (Sandbox Code Playgroud)
好吧,足够的例子.任何人都可以帮助我解决这个问题,而不会产生任何过于反本能的东西(我是图书馆开发人员,而人们喜欢习惯性的图书馆)?
- 编辑
没关系.这是最令人烦恼的解析:为什么不是A(()); 工作?.
谢谢,sbi.
| 归档时间: |
|
| 查看次数: |
4724 次 |
| 最近记录: |