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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9368985/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:44:15  来源:igfitidea点击:

How do I get g++ to compile c++11 code with a move constructor?

c++g++c++11move-constructor

提问by Collin

I can't seem to get g++ to compile c++11 code that uses a move constructor. I keep getting this error:

我似乎无法让 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&)'

The program I am writing is quite different from this, but I trimmed it down to the part that seems like it should definitely work, yet still triggers the error:

我正在编写的程序与此完全不同,但我将其修剪为看起来肯定可以工作的部分,但仍然会触发错误:

#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;
}

Can anyone tell me what I am doing wrong? Rather, how to fix it?

谁能告诉我我做错了什么?相反,如何修复它?

This is my gcc version:

这是我的 gcc 版本:

gcc (Ubuntu/Linaro 4.6.2-14ubuntu2) 4.6.2

回答by Kerrek SB

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

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

While you're at it, you might as well Do It Right and enable all warnings:

当你在做的时候,你也可以正确地做并启用所有警告:

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

You really, really shouldn't be compiling with any less, especiallyif you are going to ask questions about your code on SO :-) Various optimization flags should optionally be considered for the release version, such as -s -O2 -flto -march=native.

你真的,真的不应该编译更少,特别是如果你要在 SO 上询问关于你的代码的问题:-) 应该选择为发布版本考虑各种优化标志,例如-s -O2 -flto -march=native.

回答by Willem Hengeveld

You probably forgot to add -std=c++0xto your commandline.

您可能忘记添加-std=c++0x到命令行。