C++ 11
我试图做出vector
的std::thread
秒.以下三点的组合说我可以.
1.)根据http://en.cppreference.com/w/cpp/thread/thread/thread,
thread
默认构造函数创建一个
不代表线程的线程对象.
2.)根据http://en.cppreference.com/w/cpp/thread/thread/operator%3D,thread
的operator=
使用移动语义将[参数,即线程右值引用]的状态分配给[调用线程].
3.)根据 http://en.cppreference.com/w/cpp/container/vector/vector,仅将大小类型变量传递给向量构造函数
具有[指定数量]的值初始化(对于类的默认构造)T实例的容器.没有复制.
所以,我这样做了:
#include <iostream>
#include <thread>
#include <vector>
void foo()
{
std::cout << "Hello\n";
return;
}
int main()
{
std::vector<std::thread> vecThread(1);
vecThread.at(0) = std::thread(foo);
vecThread.at(0).join();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这在VC11和g ++ 4.8.0(在线编译器)中按预期运行,如下所示:
控制台输出:
Hello
Run Code Online (Sandbox Code Playgroud)
然后我在clang 3.2中尝试了它,通过在同一个网页上切换编译器菜单,这给出了:
stderr:
pure virtual method called
terminate called without an active exception
Run Code Online (Sandbox Code Playgroud)
当一个代表线程的线程对象在join()
编辑或detach()
编辑之前超出范围时,程序将被强制终止.我有join()
ed vecThread.at(0)
,所以剩下的唯一问题是临时线程 …
我将以下内容称为"多重继承":
我想知道它是否存在以及如何明确地访问嵌入的子对象.
1.)[ 专业C++,2 次编] †中指明了编译程序不能有直接继承其两个直接父类和所述父的父类.这是真的吗?
鉴于a GrandParent
和Parent
,扩展GrandParent
,VC12和g ++允许a GrandChild
直接从两者Parent
和GrandParent
.继承.在VC12和g ++中,可以按如下方式定义这些类:
GrandParent
声明一个int num
数据成员.除继承之外,Parent
声明自己的.除了继承's和s 之外,它还声明了它自己.num
GrandParent
num
GrandChild
num
Parent
GrandParent
num
VC12似乎允许明确的成员访问,但g ++只允许它在某些情况下.
#include <iostream>
using std::cout;
using std::endl;
struct GrandParent { int num; };
struct Parent : GrandParent { int num; };
struct GrandChild : GrandParent, Parent { int num; };
int main()
{
GrandChild gc;
gc.num = …
Run Code Online (Sandbox Code Playgroud) __func__
函数的C++ 11本地预定义变量不能使用默认的内置Visual Studio 2012(v110)编译器或2012年11月的CTP(v120_CTP_Nov2012)编译器在Visual Studio 2012 Professional(安装了Update 1)中进行编译.然而,编辑并没有抱怨任何红色波浪形下划线__func__
.__func__
在这种情况下foo
,它应该给出其包含函数的名称,但这既不编译也不让编辑抱怨:
#include <iostream>
using namespace std;
void foo()
{
cout << __func__ << endl;
return;
}
int main()
{
foo();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它给出了编译器错误:
error C2065: '__func__' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)
我在代码中遗漏了什么,或者在未来的更新中是否可以使用?
c++ standards-compliance visual-c++ c++11 visual-studio-2012
C++ 11
程序初始化一个vector
名为s的命名myVec
,int
vector
然后使用一个循环打印出每个内部vector
元素.但是当我试图看到当我使用额外的花括号时会发生什么,我得到了意想不到的结果.以下内容也适用于此LiveWorkSpace,以便在编译器之间轻松切换.g++ 4.8.0
只汇编到myVec[5]
.clang++ 3.2
汇编一切:
#include <iostream>
#include <vector>
int main()
{
std::vector<std::vector<int>> myVec =
{
/* myVec[0] */ {1, 2},
/* myVec[1] */ {},
/* myVec[2] */ {{}},
/* myVec[3] */ { {}, {} },
/* myVec[4] */ { {}, {}, {} },
/* myVec[5] */ {{{}}}
/* myVec[6] */ // , { {{}}, {{}} } // g++ 4.8.0 COMPILER ERROR
/* …
Run Code Online (Sandbox Code Playgroud) 这是一个带有未定义方法的类.似乎编译器允许构造此类的实例,只要从未调用未定义的成员函数:
struct A {
void foo();
};
int main() {
A a; // <-- Works in both VC2013 and g++
a.foo(); // <-- Error in both VC2013 and g++
}
Run Code Online (Sandbox Code Playgroud)
这是一个类似的情况,但涉及继承.子类Bar
扩展了基类Foo
.Foo
定义一个方法g()
.Bar
声明同名方法但不定义它:
#include <iostream>
struct Foo {
void g() { std::cout << "g\n"; }
};
struct Bar : Foo {
void g();
};
int main() {
Bar b; // Works in both VC2013 and g++
b.Foo::g(); // Works in both VC2013 …
Run Code Online (Sandbox Code Playgroud) 如何在没有复制或移动构造临时元素的情况下初始化数组?当元素具有显式delete
d复制或移动构造函数时,我只能在元素具有默认ctor或具有所有默认参数的ctor时初始化数组,并且我执行以下操作之一:(a)明确声明数组,(b) )直接初始化和零初始化数组,或(c)复制初始化和零初始化数组.无论是直接(但不是零)初始化,也不是复制(但不是零)初始化编译.
struct Foo
{
Foo(int n = 5) : num(n) {}
Foo(const Foo&) = delete;
//Foo(Foo&&) = delete; // <-- gives same effect
int num;
};
int main()
{
// Resultant arrays for 'a1', 'a2', and 'a3' are two
// 'Foo' elements each with 'num' values of '5':
Foo a1[2]; // plain declaration
Foo a2[2] {}; // direct initialization and zero initialization
Foo a3[2] = {}; // copy initialization and zero initialization
Foo a4[2] {5, 5}; // …
Run Code Online (Sandbox Code Playgroud) C++
如何将内部(嵌套)类的定义置于其外部(封闭)类的定义之外,其中外部类至少有一个内部类实例作为数据成员?我搜索了但我找到的最相关的 SO 答案,源文件中的嵌套类定义,没有一个示例,其中外部类将内部对象作为数据成员。我遵循了那个答案,就在外部类的定义中声明但未定义内部类而言,但我的代码仍然被破坏:
struct Outer
{
struct Inner;
Inner myinner;
Outer() : myinner(2) {}
};
struct Outer::Inner
{
Inner(int n) : num(n) {}
int num;
};
int main()
{
Outer myouter;
}
Run Code Online (Sandbox Code Playgroud)
它给出了error C2079: 'Outer::myinner' uses undefined struct 'Outer::Inner'
VC11 中的错误。
为什么损坏的代码没有与在Inner
的定义中定义的版本等效的效果Outer
,就像下面的工作代码一样?
struct Outer
{
struct Inner
{
Inner(int n) : num(n) {}
int num;
} myinner;
Outer() : myinner(2) {}
};
Run Code Online (Sandbox Code Playgroud) 我想了解访问修饰符关于继承的4种不同行为,当涉及使用和/或省略template
s和this
关键字的4种组合时.以下所有代码均在g ++ 4.8中完成:
这里有一个GrandChild
类,它private
LY从继承Parent
,这private
LY从继承GrandParent
,其中有一个public
enum
n
.非对象,客户端代码可以访问GrandParent::n
,因为后者是一个public
enum
.但是GrandParent::n
从内部无法进入GrandChild
:
#include <iostream>
using namespace std;
struct GrandParent { enum {n = 0}; };
struct Parent : private GrandParent { enum {n = 1}; };
struct GrandChild : private Parent {
enum {n = 2};
void f() {cout << GrandParent::n << endl;}
// ^ error: 'struct GrandParent GrandParent::GrandParent'
// is …
Run Code Online (Sandbox Code Playgroud) 我怎样才能传递临时数组?我想做这样的事情:
#include <iostream>
int sum(int arr[]) {
int answer = 0;
for (const auto& i : arr) {
answer += i;
}
return answer;
}
int main() {
std::cout << sum( {4, 2} ) << std::endl; // error
std::cout << sum( int[]{4, 2} ) << std::endl; // error
}
Run Code Online (Sandbox Code Playgroud)
我在函数参数的括号中是否需要正整数文字[]
?如果我包含该文字,它会限制我可以传递给那个大小的数组的数组吗?另外,如何通过rvalue引用或const引用传递数组元素?因为上面的示例没有编译,我认为制作函数的参数类型int&&[]
或const int&[]
不起作用.
这不是最烦恼的解析:为什么不是A(()); 工作?,它基于一种形式的解析A a(());
,其OP思想能够默认 - A
使用额外的括号组构造一个对象.
相比之下,我的问题是关于2个类,f
并且g
,其中f
有一个默认构造函数,而g
ctor需要一个f
.我想g
用临时f
参数调用ctor ,所有这些都不使用统一的初始化语法.std::cout
在g
ctor中有一个语句,因此缺少输出表示函数声明而不是g
对象实例化.我在注释中用3个数字注释了示例代码.#1和#2编译时#3注释掉了,反之亦然:
#include <iostream>
struct f {};
struct g {
g(f) { std::cout << "g's ctor\n"; }
};
int main() {
// -----Output-----
g( f() ); // #1: function declaration; expected
g( ( f() ) ); // #2: also a function declaration; UNEXPECTED
// g myG( ( f() ) ); …
Run Code Online (Sandbox Code Playgroud) c++ constructor class most-vexing-parse function-declaration
c++ ×10
c++11 ×3
arrays ×2
inheritance ×2
vector ×2
class ×1
constructor ×1
copy-elision ×1
curly-braces ×1
in-place ×1
nested ×1
nested-class ×1
reference ×1
stdthread ×1
templates ×1
temporary ×1
this ×1
visual-c++ ×1