小编Cod*_*cks的帖子

矢量std :: threads

C++ 11

我试图做出vectorstd::thread秒.以下三点的组合说我可以.

1.)根据http://en.cppreference.com/w/cpp/thread/thread/thread, thread默认构造函数创建一个

不代表线程的线程对象.

2.)根据http://en.cppreference.com/w/cpp/thread/thread/operator%3D,threadoperator=

使用移动语义将[参数,即线程右值引用]的状态分配给[调用线程].

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),所以剩下的唯一问题是临时线程 …

c++ multithreading vector c++11 stdthread

14
推荐指数
1
解决办法
2万
查看次数

什么是多重继承?

我将以下内容称为"多重继承":

  • 通过继承一个或多个后代,直接继承一次类,间接继承一次或多次
  • 通过继承两个或多个后代来间接继承一个类两次或更多次

我想知道它是否存在以及如何明确地访问嵌入的子对象.

1.)[ 专业C++,2 编] 中指明了编译程序不能有直接继承其两个直接父类和所述父的父类.这是真的吗?

鉴于a GrandParentParent,扩展GrandParent,VC12和g ++允许a GrandChild直接从两者ParentGrandParent.继承.在VC12和g ++中,可以按如下方式定义这些类:

GrandParent声明一个int num数据成员.除继承之外,Parent声明自己的.除了继承's和s 之外,它还声明了它自己.numGrandParentnumGrandChildnumParentGrandParentnum

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)

c++ inheritance multiple-inheritance diamond-problem

14
推荐指数
1
解决办法
964
查看次数

__func__ C++ 11函数的本地预定义变量,不会编译

__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

13
推荐指数
1
解决办法
1万
查看次数

整数向量向量的统一初始化

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)

c++ vector curly-braces uniform-initialization c++11

11
推荐指数
1
解决办法
1202
查看次数

是否允许使用未使用的未定义方法?

这是一个带有未定义方法的类.似乎编译器允许构造此类的实例,只要从未调用未定义的成员函数:

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)

c++ virtual-functions language-lawyer undefined-function

8
推荐指数
1
解决办法
373
查看次数

如何就地初始化数组?

如何在没有复制或移动构造临时元素的情况下初始化数组?当元素具有显式deleted复制或移动构造函数时,我只能在元素具有默认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++ arrays in-place deleted-functions copy-elision

6
推荐指数
1
解决办法
4181
查看次数

外部类的嵌套类定义,而外部类包含内部类的实例

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)

c++ nested nested-class inner-classes outer-classes

5
推荐指数
1
解决办法
1919
查看次数

访问修饰符在继承中的不同行为取决于"this"关键字和模板或缺少

我想了解访问修饰符关于继承的4种不同行为,当涉及使用和/或省略templates和this关键字的4种组合时.以下所有代码均在g ++ 4.8中完成:

这里有一个GrandChild类,它privateLY从继承Parent,这privateLY从继承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)

c++ inheritance templates access-modifiers this

5
推荐指数
1
解决办法
415
查看次数

如何传递临时数组?

我怎样才能传递临时数组?我想做这样的事情:

#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&[]不起作用.

c++ arrays reference temporary parameter-passing

5
推荐指数
1
解决办法
2574
查看次数

最烦恼的解析:为什么不是`g((f()));`调用`f`的默认构造函数并将结果传递给带有`f`的`g`的ctor?

这不是最烦恼的解析:为什么不是A(()); 工作?,它基于一种形式的解析A a(());,其OP思想能够默认 - A使用额外的括号组构造一个对象.

相比之下,我的问题是关于2个类,f并且g,其中f有一个默认构造函数,而gctor需要一个f.我想g用临时f参数调用ctor ,所有这些都不使用统一的初始化语法.std::coutgctor中有一个语句,因此缺少输出表示函数声明而不是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

4
推荐指数
1
解决办法
162
查看次数