我上课了
class A
{
public:
class Key
{
Key() {}
Key(Key const &) {}
};
A(Key key, int a = 5) {}
};
Run Code Online (Sandbox Code Playgroud)
构造函数Key是私有的,因此没有人应该能够构造一个对象A.但是,使用以下代码:
int main() {
A a(A::Key()); // this compiles !!!
A a2(A::Key(), 5); // this doesn't
// somehow defaulting the argument causes the private constructor
// to be OK - no idea why
return 0;
}
Run Code Online (Sandbox Code Playgroud)
通过int a在我的构造函数中使用默认参数,编译器很乐意编译我的用法,A::Key()尽管它是私有的.但是,如果我明确地给出了一个值a,那么编译器会正确识别我正在尝试使用私有构造函数并输出错误.为什么是这样?有没有办法强制编译器在第一个例子中出错?
请看这里的实例.
考虑这个例子:
#include <iostream>
#include <string>
#include <vector>
#include <iterator>
int main()
{
std::string sen = "abc def ghi jkl";
std::istringstream iss(sen);
std::vector<std::string> // declaration in question
vec(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>());
std::copy(vec.begin(), vec.end(),
std::ostream_iterator<std::string>(std::cout, "\n"));
}
Run Code Online (Sandbox Code Playgroud)
编译器在调用时抛出错误 std::copy
request for member 'begin' in 'vec', which is of non-class type...
我可以解决这个错误:
std::istream_iterator<std::string> it_begin(iss);
std::istream_iterator<std::string> it_end;
std::vector<std::string> vec(it_begin, it_end);
Run Code Online (Sandbox Code Playgroud)
或者在每个参数周围加上括号,如下所示:
std::vector<std::string>
vec((std::istream_iterator<std::string>(iss)),
(std::istream_iterator<std::string>()));
Run Code Online (Sandbox Code Playgroud)
甚至在C++ 11中使用新的统一初始化:
std::vector<std::string> vec { /*begin*/, /*end*/ };
Run Code Online (Sandbox Code Playgroud)
为什么编译器将示例中的声明解析为函数声明?我知道最烦恼的解析,但我认为只有空参数列表才会发生.我也想知道为什么第二种解决方法有效.
我有这个简单的C++问题,这让我想重新尝试重新学习CS学位,这次尝试学习一些东西.;)
为什么这段代码不能编译:
vector<int> v(int());
v.push_back(1);
Run Code Online (Sandbox Code Playgroud)
而另一个编译没有一个警告
vector<int> v((int()));
v.push_back(1);
Run Code Online (Sandbox Code Playgroud)
甚至很难找到差异(增加了额外的括号:P).
我有一些用户定义的迭代器,偶尔我会得到一个很容易解决的奇怪错误,但我不明白为什么我会得到它:
uint8_t bytes[pitch*height];
array_iterator::col_iterator a( &bytes[0] );
array_iterator::row_iterator base_iter_begin(
array_iterator::col_iterator( &bytes[0] ), width, pitch );
array_iterator::row_iterator base_iter_end(
array_iterator::col_iterator( &bytes[pitch*height] ), width, pitch
);
Run Code Online (Sandbox Code Playgroud)
我有一个名为array_iterator的类,嵌入了typedefs row_iterator和col_iterator.row_iterator构造函数将col_iterator作为其第一个参数.第一个和最后一个语句工作正常.中间语句无法编译,并出现以下错误:
test-2d-iterators.cc:780: error: declaration of 'bytes' as array of references
Run Code Online (Sandbox Code Playgroud)
写入&(字节[0])并不能解决问题(不出所料,因为[]的优先级高于&).当然,我可以用"a"代替显式col_iterator构造函数调用,但为什么我必须这样做?如果有问题,为什么最后一行中的col_iterator构造函数会编译?
谢谢.
为什么编译器将此行解释为函数定义而不是变量定义:
Y y(X());
Run Code Online (Sandbox Code Playgroud)
在以下代码中:
#include <iostream>
struct X {
X() { std::cout << "X"; }
};
struct Y {
Y(const X &x) { std::cout << "Y"; }
void f() { std::cout << "f"; }
};
int main() {
Y y(X());
y.f();
}
Run Code Online (Sandbox Code Playgroud)
VS2010在"yf();"行上给出以下错误:
left of '.f' must have class/struct/union
Run Code Online (Sandbox Code Playgroud)
标准的哪一部分描述了这种行为?以下问题的答案并未提供有关它的详细信息: 最令人烦恼的解析
考虑:
struct Foo {
enum { bar };
explicit Foo(int){}
};
struct Baz { explicit Baz(Foo){} };
Baz b(Foo(Foo::bar)); // #1
Run Code Online (Sandbox Code Playgroud)
第1行是最令人烦恼的解析,即使它Foo::bar是一个限定ID并且不可能是一个有效的参数名称?Clang和GCC不同意 ; 哪个编译器是正确的?
我正在阅读这篇文章
第3项中有一条声明说
// C++98
rectangle w( origin(), extents() ); // oops, vexing parse
Run Code Online (Sandbox Code Playgroud)
以上是一个最令人烦恼的解析.如果我做了这样的事情
struct origin
{
};
struct Rectangle
{
Rectangle(const origin& s)
{
}
};
Run Code Online (Sandbox Code Playgroud)
该声明
Rectangle s(origin());
Run Code Online (Sandbox Code Playgroud)
工作得很好,并不像一个令人讨厌的解析.为什么作者说这是一个令人烦恼的解析.这是一个错字还是我错过了什么?
此代码导致编译错误(最令人烦恼的解析)
#include <iostream>
class A {
int a;
public:
A(int x) :a(x) {}
};
class B {
public:
B(const A& obj) { std::cout << "B\n";}
void foo() {std::cout << "foo\n";}
};
int main()
{
int test = 20;
B var(A(test)); //most vexing parse
var.foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我传递20而不是test(A(20)而不是A(test)),则没有编译错误.
#include <iostream>
class A {
int a;
public:
A(int x) :a(x) {}
};
class B {
public:
B(const A& obj) { std::cout << "B\n";} …Run Code Online (Sandbox Code Playgroud) 是否可以(对A类进行任何修改)进行以下工作?即,使最令人烦恼的解析出错?
class A {
};
int main() {
A a(); // can this be forced to be an error??
A b; // this should work
}
Run Code Online (Sandbox Code Playgroud) 我无法理解为什么以下函数不能编译
#include <iostream>
#include <map>
int main(){
std::map<int, int, std::less<int>> myMap(std::less<int>());
myMap[2] = 2;
std::cout << myMap[2] << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息如下——
std_less_check.cpp: In function ‘int main()’:
std_less_check.cpp:6:10: warning: pointer to a function used in arithmetic [-Wpointer-arith]
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: assignment of read-only location ‘*(myMap + 2)’
myMap[2] = 2;
^
std_less_check.cpp:6:14: error: cannot convert ‘int’ to ‘std::map<int, int, std::less<int> >(std::less<int> (*)())’ in assignment
std_less_check.cpp:7:23: warning: pointer to a function used in arithmetic [-Wpointer-arith]
std::cout << …Run Code Online (Sandbox Code Playgroud)