标签: clang++

'static void'作为decl-specifier

以下代码用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?

谢谢,

c++ clang c++11 clang++

5
推荐指数
1
解决办法
173
查看次数

模板函数中的volatile类型推导有什么问题?

我有一个模板功能:

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是易变的,但不是指向的对象.

c++ templates g++ volatile clang++

5
推荐指数
1
解决办法
505
查看次数

使用g ++和clang ++调用Integral模板成员函数时出错

我目前陷入编译错误,我无法确定...

这是一个最小的工作示例:

#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)

c++ gcc templates member clang++

5
推荐指数
1
解决办法
143
查看次数

自引用unordered_map会导致gcc 5.3出现问题,但不会导致clang问题

以下代码无法在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)

c++ gcc libstdc++ clang++ libc++

5
推荐指数
1
解决办法
277
查看次数

与static_assert和boost :: hana相关的Clang编译错误

考虑以下在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)

c++ static-assert clang++ boost-hana

5
推荐指数
1
解决办法
511
查看次数

用C ++ 17 Mac编译

我无法使用-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++ macos gcc g++ clang++

5
推荐指数
1
解决办法
4905
查看次数

模板中的thread_local可能存在clang编译错误

在我的项目中,我需要为数据成员的每个实例单独的线程本地存储.因为我在实现此功能时遇到了问题,所以我将代码的简化版本提取到以下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)

c++ macos multithreading clang++ c++14

5
推荐指数
0
解决办法
270
查看次数

Android NDK CMake使用C ++ 17

问题

我想在我的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.build

cppFlags此处的切换是上述构建日志摘录中第二个标志的来源。我知道这里的某些版本已过时。但是我是从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)

android cmake android-ndk clang++ c++17

5
推荐指数
1
解决办法
2727
查看次数

显式默认的具有别名类型的特殊成员函数 - Clang vs. GCC

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的实例).

  1. 有明确定义的行为吗?如果是,哪个编译器是对的
  2. 是否适合为GCC或Clang提交错误报告?

c++ g++ c++11 clang++

5
推荐指数
0
解决办法
67
查看次数

C++构造函数采用大小为1的std :: initializer_list

下面的示例程序代码背后的想法是说明如果初始化程序列表构造函数在给定恰好一个元素时不等同于默认复制构造函数,则可能导致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)

c++ g++ initializer-list clang++

5
推荐指数
1
解决办法
154
查看次数