C ++ 11成员初始化列表模糊性 [英] C++11 member initialization list ambiguity
本文介绍了C ++ 11成员初始化列表模糊性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
由于此环境中的GNU标准库实现,我在尝试在c ++ 11符号解析中出现歧义:
- Arch Linux 4.2.5-1(x86_64)
- g ++ 5.2.0
- clang ++ 3.7.0 b
示例:
#include< iostream&
#include< string>
结构版本{
无符号专业;
unsigned minor;
unsigned patch;
版本(unsigned major,unsigned minor,unsigned patch):
major(major),minor(minor),patch(patch){}
friend std :: ostream& operator<<<(std :: ostream& out,version const& v){
out< v.major<< 。
out<< v.minor < 。
out<< v.patch;
return out;
}
};
int main(int argc,char ** argv){
version v(1,1,0);
std :: cout<< v<< std :: endl;
return 0;
}
编译器错误:
错误:成员初始化程序'gnu_dev_major'未命名非静态数据
成员或基类
错误:成员初始化程序'gnu_dev_minor'静态数据
成员或基类
命令:
clang ++ -std = c ++ 11 -o test * .cpp
范围解析运算符似乎不适用于成员初始化列表,因此我无法弄清楚如何解决歧义。
解决方案
另一种方法是使用大括号:
版本(unsigned major,unsigned minor,unsigned patch):
major {major},minor {minor},patch {patch} {}
然后宏不会干扰,因为它们是类函数宏,需要调用括号。 / p>
I am struggling with what appears to be an ambiguity in c++11 symbol resolution due to the GNU standard library implementation in this environment:
- Arch Linux 4.2.5-1 (x86_64)
- g++ 5.2.0
- clang++ 3.7.0
Example:
#include <iostream>
#include <string>
struct version {
unsigned major;
unsigned minor;
unsigned patch;
version(unsigned major, unsigned minor, unsigned patch) :
major(major), minor(minor), patch(patch) { }
friend std::ostream & operator<<(std::ostream & out, version const& v) {
out << v.major << ".";
out << v.minor << ".";
out << v.patch;
return out;
}
};
int main(int argc, char ** argv) {
version v(1, 1, 0);
std::cout << v << std::endl;
return 0;
}
Compiler error:
error: member initializer 'gnu_dev_major' does not name a non-static data
member or base class
error: member initializer 'gnu_dev_minor' does not name a non-static data
member or base class
Command:
clang++ -std=c++11 -o test *.cpp
The scope resolution operator does not appear to be applicable in member initialization lists so I can't figure out how to resolve the ambiguity. This sample compiles fine without the c++11 flag.
解决方案
Another way is to use braces:
version(unsigned major, unsigned minor, unsigned patch) :
major{major}, minor{minor}, patch{patch} { }
Then the macros will not interfere because they are function-like macros and need parentheses to be invoked.
这篇关于C ++ 11成员初始化列表模糊性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文