我正在使用clang 3.6' - std = c ++ 1z'来尝试折叠表达式,但是我不太了解.我正在测试的功能是:
auto minus = [](auto... args) { return (args - ...); };
...
std::cout << minus(10, 3, 2) << std::endl;
Run Code Online (Sandbox Code Playgroud)
根据n4191,我期待它作为左折叠扩展
(10 - 3) - 2
Run Code Online (Sandbox Code Playgroud)
然而,给出结果5,结果是9,这似乎是正确的折叠扩展,即
10 - (3 - 2)
Run Code Online (Sandbox Code Playgroud)
我错过了什么或误解了n4191吗?谢谢
我最近发现了这段代码:
struct Foo{};
int main()
{
Foo a;
// clang++ deduces std::initializer_list
// g++5.1 deduces Foo
auto b{a};
a = b;
}
Run Code Online (Sandbox Code Playgroud)
它用g ++ 5.1编译得很好,但是在clang ++中失败了(使用了两个-std=c++11和-std=c++14相同的结果).其原因是,铛++推导的类型b如std::initializer_list<Foo>,而g++5.1推导出作为Foo.据我所知,类型确实应(反直觉确实)std::initializer_list在这里.为什么g ++ 5推断出类型Foo?
我刚刚安装了ClangOnWin,我正试图让clang-tidy"现代化"检查工作.不幸的是,clang-tidy似乎并不了解它们:clang-tidy -list-checks foo.cpp -- | grep modernize没有输出.
这里列出了 "现代化"检查,但该页面似乎记录了Clang 3.8,我安装的版本是3.7.但是,3.7版是LLVM下载页面中列出的当前版本.
clang-tidy知道各种安全检查,所以我认为我已正确安装.例如,clang-tidy -list-checks foo.cpp -- | grep security产生这个:
clang-analyzer-security.FloatLoopCounter
clang-analyzer-security.insecureAPI.UncheckedReturn
clang-analyzer-security.insecureAPI.getpw
clang-analyzer-security.insecureAPI.gets
clang-analyzer-security.insecureAPI.mkstemp
clang-analyzer-security.insecureAPI.mktemp
clang-analyzer-security.insecureAPI.rand
clang-analyzer-security.insecureAPI.strcpy
clang-analyzer-security.insecureAPI.vfork
Run Code Online (Sandbox Code Playgroud)
是否有一些特殊的东西我需要做以启用检查,如modernize-use-override和modernize-use-nullptr?
请考虑以下代码:
template<typename>
struct One {};
template<typename, typename>
struct Two {};
template<template<typename...> class TTP, typename...>
struct SS;
#ifdef TEST_TTP
template<template<typename> class OneParam,
typename... Ts>
struct SS<OneParam, Ts...> {};
template<template<typename, typename> class TwoParam,
typename... Ts>
struct SS<TwoParam, Ts...> {};
#else // TEST_TTP
template<template<typename> class OneParam,
typename TParam>
struct SS<OneParam, TParam> {};
template<template<typename, typename> class TwoParam,
typename TParam1,
typename TParam2>
struct SS<TwoParam, TParam1, TParam2> {};
#endif // TEST_TTP
int main() {
SS<One, int> ssoi;
SS<Two, int, int> sstii;
}
Run Code Online (Sandbox Code Playgroud)
如果TEST_TTP未定义,此代码将在Clang,GCC和MSVC上正确编译.但是,如果它被 …
(有关我正在使用的特定版本的Boost和Clang的信息,请参阅问题的结尾)
使用新的实验性-fmodules功能在master/HEAD中编译Clang,使用下面显示的命令行选项编译以下文件时出现构建错误:
#include <iterator>
#include <boost/move/iterator.hpp>
Run Code Online (Sandbox Code Playgroud)
编译命令和错误:
anhall@leviathan: <path-to-clang-install-from-master>/bin/clang++ -o file.o -c file.cpp --std=c++1z -stdlib=libc++ -fmodules
In file included from file.cpp:2:
In file included from /usr/local/include/boost/move/iterator.hpp:27:
/usr/local/include/boost/move/detail/iterator_traits.hpp:60:17: error: reference to 'random_access_iterator_tag' is ambiguous
typedef std::random_access_iterator_tag iterator_category;
^
/Users/anhall/impersonal/code/llvm-reflexpr/install/bin/../include/c++/v1/iterator:438:30: note: candidate found by name lookup is 'std::__1::random_access_iterator_tag'
struct _LIBCPP_TYPE_VIS_ONLY random_access_iterator_tag : public bidirectional_iterator_tag {};
^
/usr/local/include/boost/move/detail/iterator_traits.hpp:34:8: note: candidate found by name lookup is 'std::random_access_iterator_tag'
struct random_access_iterator_tag;
^
/usr/local/include/boost/move/detail/iterator_traits.hpp:71:17: error: reference to 'random_access_iterator_tag' is ambiguous
typedef std::random_access_iterator_tag iterator_category;
^
/Users/anhall/impersonal/code/llvm-reflexpr/install/bin/../include/c++/v1/iterator:438:30: …Run Code Online (Sandbox Code Playgroud) 第一个版本通过将值从内存移动到局部变量来进行优化.第二个版本没有.
我原以为编译器可能会选择在这里进行localValue优化,而不是在循环的每次迭代中从内存中读取和写入值.为什么不呢?
class Example
{
public:
void processSamples(float * x, int num)
{
float localValue = v1;
for (int i = 0; i < num; ++i)
{
x[i] = x[i] + localValue;
localValue = 0.5 * x[i];
}
v1 = localValue;
}
void processSamples2(float * x, int num)
{
for (int i = 0; i < num; ++i)
{
x[i] = x[i] + v1;
v1 = 0.5 * x[i];
}
}
float v1;
};
Run Code Online (Sandbox Code Playgroud)
processSamples组装成代码如下:
.L4:
addss xmm0, DWORD PTR …Run Code Online (Sandbox Code Playgroud) 样例代码:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::regex npat(R"(^(\d+))");
std::smatch m;
std::regex_search(std::string("10"), m, npat);
std::cout << m.size() << " m.str(1): |"
<< m.str(1) << "| ";
std::cout << std::stoi(m.str(1)) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
当使用
g++ -std=c++11 main.cpp
Run Code Online (Sandbox Code Playgroud)
输出是
2 m.str(1): |10| 10
Run Code Online (Sandbox Code Playgroud)
这是预期的。
但是,当使用
g++ -std=c++11 -O1 main.cpp
Run Code Online (Sandbox Code Playgroud)
输出变为
libc++abi.dylib: terminating with uncaught exception
of type std::invalid_argument: stoi: no conversion
2 m.str(1): || Abort trap: 6
Run Code Online (Sandbox Code Playgroud)
编译器版本:
g++ -v
Run Code Online (Sandbox Code Playgroud)
配置为:--prefix = / Library / Developer / CommandLineTools / …
我目前的文件结构是:
??? common
? ??? example.cc
??? compile_commands.json
??? include
??? common
??? example.hh
Run Code Online (Sandbox Code Playgroud)
example.hh: 留空
example.cc:
??? common
? ??? example.cc
??? compile_commands.json
??? include
??? common
??? example.hh
Run Code Online (Sandbox Code Playgroud)
compile_commands.json:
#include "common/example.hh"
int main() {
return 0;
}
Run Code Online (Sandbox Code Playgroud)
打开example.cc它会产生错误:
'common/example.hh' file not found clang(pp_file_not_found)
Run Code Online (Sandbox Code Playgroud)
我直接运行命令,它工作正常:
[
{
"directory": "/home/user/project",
"file": "/home/user/project/common/example.cc",
"arguments": [
"/usr/bin/clang++",
"-I /home/user/project/include",
"-o example",
"/home/user/project/common/example.cc"
],
"output": "example"
}
]
Run Code Online (Sandbox Code Playgroud)
环境信息:
$ clang++ --version
clang version 10.0.0-4ubuntu1
Target: x86_64-pc-linux-gnu
Thread …Run Code Online (Sandbox Code Playgroud) 两个月前,我报作为一个clang++错误,那下面套C ++程序z,以4294967295在编译时clang++ -O2 -fno-strict-enums。
enum e { e1, e2 } e;
long long x, y, z;
char *p;
void f(void) {
e = (enum e) 4294967295;
x = (long long) e;
y = e > e1;
z = &p[e] - p;
}
Run Code Online (Sandbox Code Playgroud)
由于程序未定义,我的错误报告因无效而关闭。我的感觉是使用该选项-fno-strict-enums使其定义。
据我所知,Clang 没有名副其实的文档,因为它的目标是在它接受的选项及其含义方面与 GCC 兼容。我读了 GCC 的选项文档-fno-strict-enums说程序应该将值设置z为-1:
-fstrict-枚举
允许编译器使用以下假设进行优化:枚举类型的值只能是枚举值之一(如 C++ 标准中所定义;基本上,可以用最少位数表示的值来表示所有枚举器)。如果程序使用强制转换将任意整数值转换为枚举类型,则此假设可能无效。
请注意,仅记录了该选项-fstrict-enums,但似乎很清楚-fno-strict-enums禁用-fstrict-enums启用的编译器行为。我无法针对 GCC 的文档提交错误,因为生成设置z为的二进制文件, …
我试图结合嵌套类和前向声明来保持代码清晰,即使它有一个非常复杂的类结构。前向声明允许我减少此处的缩进级别。
以下代码在 g++-9.3 和 clang++-10 上都可以很好地编译:
class A {
public:
class B;
};
class A::B {
public:
int foo=0;
};
Run Code Online (Sandbox Code Playgroud)
但是,当我在另一个类中嵌套执行相同的操作时,此构造在 g++ 上没有任何警告地工作,但在 clang++ 上失败:
class Outer {
public:
class A {
public:
class B;
};
class A::B {
public:
int foo=0;
};
};
Run Code Online (Sandbox Code Playgroud)
clang++ 的失败是:
test.cpp:7:16: error: non-friend class member 'B' cannot have a qualified name
class A::B {
~~~^
1 error generated.
Run Code Online (Sandbox Code Playgroud)
我想这在某种程度上是无效的代码,gcc 足够仁慈以正确解释?我意识到我可以直接在 A 类中移动类定义,但假设我想保留这个版本的前向声明。
clang++ ×10
c++ ×8
clang ×4
g++ ×3
c++11 ×2
c++17 ×2
auto ×1
boost ×1
c++-modules ×1
clang-tidy ×1
clangd ×1
enums ×1
gcc5 ×1
optimization ×1
regex ×1
visual-c++ ×1