在我最近评论的一段代码中,编译好了g++-4.6,我遇到了一个奇怪的尝试来创建一个std::shared_ptr来自std::unique_ptr:
std::unique_ptr<Foo> foo...
std::make_shared<Foo>(std::move(foo));
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎很奇怪.这应该是std::shared_ptr<Foo>(std::move(foo));afaik,虽然我对动作并不完全熟悉(而且我知道std::move只是演员,没有任何动静).
检查此SSC上的不同编译器(NUC*)E
#include <memory>
int main()
{
std::unique_ptr<int> foo(new int);
std::make_shared<int>(std::move(foo));
}
Run Code Online (Sandbox Code Playgroud)
编译结果:
所以问题是:哪个编译器在标准方面是正确的?标准是否要求这是一个无效的声明,一个有效的声明,或者这是不确定的?
加成
我们已经同意这些编译器中的一些,例如clang ++和g ++ - 4.6.4,允许转换,而不应该.但是对于g ++ - 4.7.3(会产生内部编译器错误std::make_shared<Foo>(std::move(foo));),请正确拒绝int bar(std::move(foo));
由于这种巨大的行为差异,我将问题保持不变,尽管其中一部分将与减少相关int bar(std::move(foo));.
*)NUC:不是普遍可编辑的
我试图用Clang在Fedora 20上编译简单的hello world,我得到以下输出:
d.cpp:1:10:致命错误:找不到'iostream'文件
#include <iostream>
我不知道如何解决它.
我写了一篇详细的教程,其中包含了这个问题的前两个答案:http://blog.johannesmp.com/2015/09/01/installing-clang-on-windows-pt1/
在Windows上,给出以下程序:
#include <iostream>
int main()
{
int arr[] = {1, 2, 3, 4, 5};
for(auto el : arr)
{
std::cout << el << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我希望能够做到以下几点:
clang++ hello.cpp -o hello.exe -std=c++14
Run Code Online (Sandbox Code Playgroud)
并得到一个64位可执行文件,只是工作.我不想附加大量的-I包含来告诉clang在哪里找到iostream或其他标准的c ++标题; 我不想在多个步骤中进行链接.
我不太关心性能,效率,使用什么链接器等等.我只是想能够让clang/gcc /无论设置正确,以便我可以使用单个,快速和脏的控制台命令,如上面的一个.
我需要安装什么才能正常工作?
作为一个主要的mac/linux用户,我习惯于只使用包管理器来安装最新版本的clang,这只是有效的.
我现在正试图在Windows上设置clang/gnu编译器,这似乎要困难得多,如果只是因为那里几乎没有简单的文档(我已经能够找到)
我试过按照这个教程:https://yongweiwu.wordpress.com/2014/12/24/installing-clang-3-5-for-windows - 并且能够使用它来获得构建和链接的clang (使用gcc 4.8.2),但生成的二进制文件是32位.
我已经尝试安装最新的prengilt clang(3.6.2)和64位版本的mingw-w64(4.9.3用64位posix和sjlj用于例外),我得到:
hello.cpp:1:10: fatal error: 'iostream' file not …Run Code Online (Sandbox Code Playgroud) 据我所知,Visual Studio 2015附带了clang.首先我虽然这只适用于Android和iOS应用,但根据这篇文章,它也应该可以使用clang ++前端用于Windows程序.但是,我找不到相应的选项.
那么请你解释一下,如何在c ++项目中改变用过的编译器(在VS2015 RC社区版中).
是否有可能让Travis CI与能够使用C++ 11的Clang一起工作?(我想要Clang,而不是GCC,我已经在Travis CI中使用了GCC 4.8.)看来预安装的版本不支持C++ 11.我安装任何较新版本的所有尝试都因此失败:
In file included from /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/bits/move.h:57:
/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../../include/c++/4.8/type_traits:269:39: error:
use of undeclared identifier '__float128'
struct __is_floating_point_helper<__float128>
Run Code Online (Sandbox Code Playgroud)
我见过这个-D__STRICT_ANSI__伎俩,但与我的其他事情发生了冲突.
是否有可能让它运作?另见我的.travis.yml.
考虑以下 C++17 代码:
#include <iostream>
int read;
int main(){
std::ios_base::sync_with_stdio(false);
std::cin >> read;
}
Run Code Online (Sandbox Code Playgroud)
它可以在使用 GCC 11.2 和 Clang 12.0.1 的Godbolt上编译并正常运行,但如果使用密钥编译,则会导致运行时错误-static。
据我了解,有一个名为 POSIX(?) 的函数read(请参阅man read(2)),因此上面的示例实际上会调用 ODR 违规,并且即使在没有-static. 如果我尝试命名变量,GCC 甚至会发出警告malloc:built-in function 'malloc' declared as non-function
上面的程序是有效的 C++17 吗?如果没有,为什么?如果是,是否是编译器错误导致其无法运行?
我有这个代码:
#include <array>
int main(int, char **argv)
{
std::array<int, 3> a = {1,2,3};
}
Run Code Online (Sandbox Code Playgroud)
这编译很好(-std = c ++ 11),但如果你包含-Wall它会发出我不明白的警告:
clang_pp_error.cpp:5:28: warning: suggest braces around initialization of subobject [-Wmissing-braces]
std::array<int, 3> a = {1,2,3};
^~~~~
{ }
Run Code Online (Sandbox Code Playgroud) 我有以下C++ 11代码.
#include <type_traits>
using IntType = unsigned long long;
template <IntType N> struct Int {};
template <class T>
struct is_int : std::false_type {};
template <long long N>
struct is_int<Int<N>> : std::true_type {};
int main()
{
static_assert (is_int<Int<0>>::value, "");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Clang ++ 3.3编译代码,但在g ++ 4.8.2静态断言失败
$ g++ -std=c++11 main.cpp
main.cpp: In function ‘int main()’:
main.cpp:15:5: error: static assertion failed:
static_assert (is_int<Int<0>>::value, "");
^
$
Run Code Online (Sandbox Code Playgroud)
问题是由不同的积分模板参数引起的.在这种情况下哪个编译器是正确的?
我正在尝试使用Clang-3.5编译以下简单的C++代码:
test.h:
class A
{
public:
A();
virtual ~A() = 0;
};
Run Code Online (Sandbox Code Playgroud)
test.cc:
#include "test.h"
A::A() {;}
A::~A() {;}
Run Code Online (Sandbox Code Playgroud)
我用来编译它的命令(Linux,uname -r:3.16.0-4-amd64):
$clang-3.5 -Weverything -std=c++11 -c test.cc
Run Code Online (Sandbox Code Playgroud)
而我得到的错误:
./test.h:1:7: warning: 'A' has no out-of-line virtual method definitions; its vtable will be emitted in every translation unit [-Wweak-vtables]
Run Code Online (Sandbox Code Playgroud)
任何暗示为什么会发出警告?虚拟析构函数根本没有内联.恰恰相反,test.cc中提供了一个外联定义.我在这里错过了什么?
编辑
我不认为这个问题是重复的:
clang的-Wweak-vtables是什么意思?
正如FilipRoséen所说.在我的问题中,我特别提到纯抽象类(在建议的副本中没有提到).我知道如何-Wweak-vtables使用非抽象类,我很好.在我的例子中,我在实现文件中定义了析构函数(它是纯抽象的).这应该可以防止Clang发出任何错误,即使是-Wweak-vtables.
我错误地使用了这个在链接步骤失败的命令:
$ clang -std=c++11 -stdlib=libc++ myInputFile.cpp
任何人都可以解释为什么clang提供了C++语言选项,以及为什么它无法链接?为什么不选择-x c++或-std=c++11完成同样的事情clang++?谢谢!