'A(tmpVector);'如何与"A tmpVector"相同?
How is 'A(tmpVector);' the same as 'A tmpVector;'?
这个问题有以下代码片段:
A::A(const char *pc) {
A(string(pc));
}
A::A(string s) {
vector<string> tmpVector;
tmpVector.push_back(s);
A(tmpVector); // <-- error
}
// Constructor
A::A(vector<string> filePathVector) {
}
问题是A(tmpVector);
与vector<string> tmpVector;
:冲突
error: conflicting declaration 'A tmpVector'
error: 'tmpVector' has a previous declaration as 'std::vector<std::basic_string<char> > tmpVector'
答案是:
这个
A(tmpVector(;
和这个一样
tmpVector;//但是已经有一个叫做tmpVector 的对象了
添加评论:
在这种情况下,((是多余的。
我的问题是:为什么括号是多余的?C++11规范中究竟是什么让它如此?我以前从未见过这种情况。
来自标准的§8[dcl.dell]:
声明器的语法为:
declarator:
ptr-declarator
noptr-declarator parameters-and-qualifiers trailing-return-type
ptr-declarator:
noptr-declarator
ptr-operator ptr-declarator
noptr-declarator:
declarator-id attribute-specifier-seq_opt
noptr-declarator parameters-and-qualifiers
noptr-declarator [ constant-expression_opt] attribute-specifier-seq_opt
( ptr-declarator )
(省略语法的剩余部分(。
特别要注意的是,
- 3是CCD_ 4
- 形式为
( ptr-declarator )
的事物是noptr-declarator
,而CCD_6又是ptr-declarator
换句话说,您可以拥有任意数量的括号,它仍然是一个声明符。现在,这在T(x);
这样的情况下会引起歧义,通过标准的§6.8[stmt.ambig]解决
涉及表达语句的语法有歧义和声明:具有函数样式的表达式语句显式类型转换(5.2.3(作为其最左边的子表达式可以是与第一个声明符开始的声明无法区分带有(。在这种情况下,该语句是一个声明。
该段所附的例子直接涵盖了这种情况:
class T {
// ...
public:
T();
T(int);
T(int, int);
};
T(a); // declaration
T(*b)(); // declaration
T(c)=7; // declaration
T(d),e,f=3; // declaration
extern int h;
T(g)(h,2); // declaration
相关文章:
- std::具有相同基类的类的变体
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 不断收到相同的错误...怎么了?C++
- 具有最大子序列大小的序列,每个元素都相同
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 在调用FreeLibrary后,释放动态链接到具有相同版本的CRT堆的DLL的内存
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- c++类声明时,相同的例程,不同的成员变量类型
- 不同语言中相同代码的不同行为
- 如何检查一个c++字符串中有多少相同的字符/数字
- visual是否可以在c++中创建一个接收无限数量相同类型(或至少相当数量)参数的函数
- c++找不到具有相同哈希的无序集合元素
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 检查函数返回类型是否与STL容器类型值相同
- 从类型std::函数传递变量失败,尽管调用方期望的类型完全相同
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- 通过交换元素使数组相同
- 使用 boost 进行标记化会给出相同的输出
- 'A(tmpVector);'如何与"A tmpVector"相同?