c++ - 如何让 g++ 使用移动构造函数编译 c++11 代码?

标签 c++ g++ c++11 move-constructor

我似乎无法让 g++ 编译使用移动构造函数的 c++11 代码。我不断收到此错误:

collin@Serenity:~/Projects/arraylib$ g++ ./t2.cpp
./t2.cpp:10:27: error: expected ‘,’ or ‘...’ before ‘&&’ token
./t2.cpp:10:38: error: invalid constructor; you probably meant ‘Blarg (const Blarg&)’

我正在编写的程序与此完全不同,但我将其精简到看起来应该可以正常工作的部分,但仍然会触发错误:

#include <iostream>

using namespace std;

class Blarg {
    public:
        Blarg () {};
        Blarg (const Blarg& original) {}; /* Copy constructor */
        Blarg (Blarg&& original) {}; /* Move constructor */
};

int main(int argc, char *argv[])
{
    Blarg b;
    return 0;
}

谁能告诉我我做错了什么?而是如何解决?

这是我的 gcc 版本:

gcc (Ubuntu/Linaro 4.6.2-14ubuntu2) 4.6.2

最佳答案

g++ -std=c++0x ./t2.cpp

当您使用它时,您不妨做对并启用所有警告:

g++ -W -Wall -Wextra -pedantic -std=c++0x -o t2 t2.cpp

您真的,真的不应该使用更少的代码进行编译,尤其是,如果您要在 SO 上询问有关您的代码的问题 :-) 应该选择为发布版本考虑各种优化标志,如 -s -O2 -flto -march=native.

关于c++ - 如何让 g++ 使用移动构造函数编译 c++11 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9368985/

相关文章:

c++ - requires 的主体是否会阻止未评估的上下文?

c++ - 从二进制文件 (C++) 读取结构的烦人错误

c++ - 无法在 Mingw-w64 中使用 "uniform_int_distribution",但 "exponential_distribution"可以使用

templates - 在使用右值引用时选择要使用的模板重载时的不同行为

c++ - 为什么 std::function 没有 function_type 或等效的成员类型?

c++ - 调用模板类的模板构造函数?

c++ - 如何使用 STL 算法组合生成器

c++ - 缩写类中定义的类型的全名

c++ - g++ Windows 链接器 "Undefined reference to..."与 SDL2

c++ - "extern C"函数从 C++ 共享对象符号表中消失