以下代码用clang编译好.我想知道C++标准是否适合这个.
class A {
static void x; // #1
static const void x; // #2
static volatile void x; // #3
};
Run Code Online (Sandbox Code Playgroud)
在我看来,没有一个声明是有效的.所述perenial C++标准验证套件 具有这样一个这样的试验(#1)和铛(V3.4)失败在于.
虽然,如果我从#1中删除静态,那么clang会按预期报告错误.
我查看了标准,并在静态数据成员(9.4.2-2)中找到了一个段落,其中说:
2在类定义中声明静态数据成员不是一个定义,除了cv-quali fi ed void之外可能是不完整的类型....
据我所知,这一行取消了#2和#3的无效,但对#1不确定.是否有更多与静态存储类声明相关的细节应该排除#1?
谢谢,
我有一个模板功能:
template <typename T>
inline void Acquire_Store(volatile T* ptr, T value) {
// ...
}
Run Code Online (Sandbox Code Playgroud)
当我尝试像这样调用它时:
volatile Node* node;
Acquire_Store(&node, static_cast<Node*>(nullptr));
Run Code Online (Sandbox Code Playgroud)
这两个g ++,clang ++编译器都是这样的:
推导出参数'T'的冲突类型('volatile List :: Node*'vs.'List :: Node*')
调用此模板函数的正确方法是什么?
更新.
现在我不确定它node的类型 - 也许,我应该改成它Node* volatile node;?
我希望变量node是易变的,但不是指向的对象.
我目前陷入编译错误,我无法确定...
这是一个最小的工作示例:
#include <iostream>
template <typename T, int R>
class a_type
{
public:
template <int N>
double segment()
{
return 42;
}
};
template <int M>
double func()
{
a_type<double, M> a;
return a.segment<1>();
}
int main(int argc, char *argv[])
{
std::cout << func<10>() << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC的错误消息如下:
g++ main.cpp -o main
main.cpp: In function 'double func()':
main.cpp:18:26: error: expected primary-expression before ')' token
return a.segment<1>();
^
main.cpp: In instantiation of 'double func() [with int M = 10]': …Run Code Online (Sandbox Code Playgroud) 以下代码无法在gcc 5.3下编译(它是从更大的代码片段中获取的简化版本):
#include <unordered_map>
#include <string>
class Foo {
std::unordered_map<std::string, Foo> m; //"self-referential"
};
int main()
{
Foo f;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
出现以下错误:
g++ --std=c++1y -c rh.cpp
In file included from /usr/local/include/c++/5.3.0/utility:70:0,
from /usr/local/include/c++/5.3.0/unordered_map:38,
from rh.cpp:1:
/usr/local/include/c++/5.3.0/bits/stl_pair.h: In instantiation of ‘struct std::pair<const int, Foo>’:
/usr/local/include/c++/5.3.0/ext/aligned_buffer.h:85:34: required from ‘struct __gnu_cxx::__aligned_buffer<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:246:43: required from ‘struct std::__detail::_Hash_node_value_base<std::pair<const int, Foo> >’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:292:12: required from ‘struct std::__detail::_Hash_node<std::pair<const int, Foo>, false>’
/usr/local/include/c++/5.3.0/bits/hashtable_policy.h:1896:60: required from ‘struct std::__detail::_Hashtable_alloc<std::allocator<std::__detail::_Hash_node<std::pair<const int, Foo>, false> …Run Code Online (Sandbox Code Playgroud) 考虑以下在Clang 3.8上成功编译的问题-std=c++14.
#include <boost/hana.hpp>
namespace hana = boost::hana;
int main() {
constexpr auto indices = hana::range<unsigned, 0, 3>();
hana::for_each(indices, [&](auto i) {
hana::for_each(indices, [&](auto j) {
constexpr bool test = (i == (j == i ? j : i));
static_assert(test, "error");
});
});
}
Run Code Online (Sandbox Code Playgroud)
测试是非常不敏感的,但这不是重点.现在考虑一个替代版本,其中测试直接放在static_assert:
#include <boost/hana.hpp>
namespace hana = boost::hana;
int main() {
constexpr auto indices = hana::range<unsigned, 0, 3>();
hana::for_each(indices, [&](auto i) {
hana::for_each(indices, [&](auto j) {
static_assert((i == (j == i ? j …Run Code Online (Sandbox Code Playgroud) 我无法使用-std = c ++ 17进行编译,我得到了:
error: invalid value 'c++17' in '-std=c++17'
Run Code Online (Sandbox Code Playgroud)
但是我更新了Xcode和clang。
我的Clang版本是:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.0.0 (clang-900.0.39.2)
Target: x86_64-apple-darwin16.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin`
Run Code Online (Sandbox Code Playgroud)
我加载了最新的标头,如可选的,我必须做
#include <experimental/optional>
Run Code Online (Sandbox Code Playgroud)
代替
#include <optional>
Run Code Online (Sandbox Code Playgroud) 在我的项目中,我需要为数据成员的每个实例单独的线程本地存储.因为我在实现此功能时遇到了问题,所以我将代码的简化版本提取到以下C++ 14程序中:
#include <iostream>
#include <unordered_map>
#include <vector>
template<class T> class ThreadLocalMember
{
public:
T& local() { return store.map[this]; }
private:
struct Store
{
Store() { std::cout << "construct" << std::endl; }
~Store() { std::cout << "destruct" << std::endl; }
std::unordered_map<ThreadLocalMember<T>*, T> map;
};
static thread_local Store store;
};
template <class T> thread_local typename ThreadLocalMember<T>::Store ThreadLocalMember<T>::store;
int main()
{
ThreadLocalMember<int> counter;
std::cout << "point 1" << std::endl;
int result = counter.local();
std::cout << "point 2; result: " << result << …Run Code Online (Sandbox Code Playgroud) 我想在我的Android NDK项目中使用分解声明。因此clang需要用调用-std=c++17。目前,我的代码可以正确编译,但是Clang打印以下警告:
warning: decomposition declarations are a C++17 extension [-Wc++17-extensions]
Run Code Online (Sandbox Code Playgroud)
在构建日志中,我发现它-std=...四次附加到构建标志:
[...]/bin/clang++ [...] -Wformat -Werror=format-security -std=c++11 -std=c++1z\
-fexceptions -std=c++1z -Wall -O0 -fno-limit-debug-info -fPIC\
-std=gnu++11 -MD -MT [...]
Run Code Online (Sandbox Code Playgroud)
我知道第二个和第三个标志来自哪里(见下文)。我试图将它们更改为-std=c++17,-std=c++1z但没有成功。
我猜以后的标记会覆盖以前的标记。所以我真的不在乎第一个。但是我不知道最后一个(-std=gnu++11)的来源以及如何停用或修改它。我也猜想它gnu++11不是c++11激活某些GNU扩展,导致我只得到警告而没有错误的情况。
但是我不能确定。不过,我想要“真正的” C ++ 17支持,而不仅仅是一些GNU扩展。我也想摆脱警告。
gradle.buildcppFlags此处的切换是上述构建日志摘录中第二个标志的来源。我知道这里的某些版本已过时。但是我是从NDK示例存储库中获得的,而且我是Android编程的新手。我还在弄清楚事情是如何进行的。所以我现在还不在乎那部分。
apply plugin: 'com.android.application'
android {
compileSdkVersion = 25
defaultConfig {
applicationId = 'com.example.stackoverflow'
minSdkVersion 14
targetSdkVersion 25
externalNativeBuild {
cmake {
arguments '-DANDROID_STL=c++_static'
cppFlags …Run Code Online (Sandbox Code Playgroud) GCC trunk(9.0)和Clang trunk(7.0)在以下代码上不一致:
template<int size_>
struct BadArray {
static constexpr int size = size_;// GCC complains due to this indirection
using Self = BadArray<size>;
Self& operator=(const Self&) noexcept = default;
};
Run Code Online (Sandbox Code Playgroud)
使用GCC进行编译失败,并显示错误消息
error: 'BadArray<size_>::Self& BadArray<size_>::operator=(const Self&)' cannot be defaulted
Run Code Online (Sandbox Code Playgroud)
而Clang接受代码(GCC 8.1和Clang 6.0的实例).
下面的示例程序代码背后的想法是说明如果初始化程序列表构造函数在给定恰好一个元素时不等同于默认复制构造函数,则可能导致Clang出现意外结果.
它还表明Clang和GCC在复制构造函数和初始化列表构造函数之间没有实现相同的优先级,这实际上使得对于可移植代码不可能使用这种类型的初始化列表构造函数.
// Include directive.
#include <initializer_list>
// The struct used in this case.
struct Foo
{
// Member value.
int val;
// Default constructor.
Foo() : val(0) {}
// Initializer list constructor.
Foo(std::initializer_list<Foo>) : val(2) {}
};
// Main function.
int main()
{
// Default constructed Foo object.
Foo foo_zero;
// It is not clear to me which constructor
// should be called by the following statement.
Foo foo_test = { foo_zero };
// Return exit code.
// Clang …Run Code Online (Sandbox Code Playgroud)