标签: clang++

C++ 11中的变量模板专业化

它是否已经实现,因为它不能编译:(使用gcc 4.7.2)

template <typename... Ts>
struct Foo {
    int foo() {
        return 0;
    }
};

template <>
struct Foo<int x, int y> {
    int foo() {
        return x * y;
    }
};

int main()
{
    Foo<2, 3> x;
    cout << x.foo() << endl; //should print 6
}
Run Code Online (Sandbox Code Playgroud)

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

7
推荐指数
1
解决办法
9224
查看次数

wcin.imbue和UTF-8

在使用g ++的linux上,如果我设置了utf8全局语言环境,那么wcin正确地将UTF-8转码为内部wchar_t编码.

但是,如果我使用经典语言环境并将UTF8语言环境灌输到wcin中,则不会发生这种情况.输入完全失败,或者每个字节独立转换为wchar_t.

使用clang ++和libc ++,既不设置全局语言环境也不设置语言环境wcin.

#include <iostream>
#include <locale>
#include <string>

using namespace std;

int main() {
    if(true)        
        // this works with g++, but not with clang++/libc++
        locale::global(locale("C.UTF-8"));
    else
        // this doesn't work with either implementation
        wcin.imbue(locale("C.UTF-8"));
    wstring s;
    wcin >> s;
    cout << s.length() << " " << (s == L"áéú");
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输入流仅包含áéú字符.(它们是UTF-8,而不是任何单字节编码).

现场演示: 两个(我不能重现在线编译器的其他行为).

这是符合标准的吗?我不应该单独留下全局区域设置imbue而是使用吗?

是否应将任何描述的行为归类为实施错误?

c++ locale g++ utf-8 clang++

7
推荐指数
1
解决办法
1830
查看次数

GNU C++和Clang中的模板实例化

看起来Clang(3.8)和GNU C++(4.9)中模板实例化的规则是不一样的.这是一个例子:

#include <cstddef>

template <bool>
class Assert {
  Assert();          // private constructor for Assert<false>
};

template <>
class Assert<true> { // implicit public constructor for Assert<true>
};

template <size_t N>
class A {
};

template <class T, size_t N>
T foo(A<N>) {
  return T(N - 1);
}

template <class T>
T foo(A<0>) {        // foo is not defined for N=0
  Assert<false>();
  return T(0);
}

int main(int argc, char **argv) {
  foo<int>(A<3>());
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

这个最小的例子显示了一个模板函数,foo它是在一个类型T和一个自然数上推广的 …

c++ gcc templates instantiation clang++

7
推荐指数
3
解决办法
628
查看次数

`if constexpr`,里面lambda,内包扩展 - 编译bug?

clang version 5.0.0 (trunk 305664)
Target: x86_64-unknown-linux-gnu
Run Code Online (Sandbox Code Playgroud)

以下代码成功编译:

template <int... A>
void f() {
    ([](auto) {
        if constexpr (A == 0)
            return 42;
        else
            return 3.14;
    }(0), ...);
}

int main() {
    f<0, 1>();
}
Run Code Online (Sandbox Code Playgroud)

......但是这个没有:

template <int... A>
void f() {
    ([](auto...) {            // Variadic lambda
        if constexpr (A == 0)
            return 42;
        else
            return 3.14;
    }(), ...);                // No argument
}

int main() {
    f<0, 1>();
}
Run Code Online (Sandbox Code Playgroud)

...屈服:

<source>:7:13: error: 'auto' in return type deduced as 'double' here …
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer compiler-bug clang++ c++17

7
推荐指数
1
解决办法
220
查看次数

编译器是否优化了净零位移?

我有一些代码,如下面的代码块(我不允许发布原始代码)在.cpp我认为由clang++(Ubuntu clang version 3.5.2-3ubuntu1 (tags/RELEASE_352/final) (based on LLVM 3.5.2))编译的文件中.
它看起来像C代码,因为我们GoogleTest用于测试我们的C代码.无论如何:

size_t const SHIFT = 4;
uint8_t var, var2;
/* Omitted: Code that sets var to, say 00011000 (base 2) */
var2 = var;
var = var << SHIFT >> SHIFT; // [1] result is 00011000 (base 2) (tested with printf)
var2 = var2 << SHIFT;
var2 = var2 >> SHIFT; // [2] result is 00001000 (base 2) (tested …
Run Code Online (Sandbox Code Playgroud)

c c++ compilation compiler-optimization clang++

7
推荐指数
1
解决办法
365
查看次数

GCC 或 Clang 是否实现 chrono::current_zone()?

Cppreference 提到函数current_zone()<chrono>. 这个功能在 GCC 或 Clang 中实现了吗?我想在在线工具之一中测试它,例如https://wandbox.orghttps://godbolt.org/,但它似乎丢失了。

c++ g++ clang++ c++20

7
推荐指数
1
解决办法
1755
查看次数

即使传递了 -fno-lto ,lld 也会运行 LTO

我有一个 CMake 项目,其中有几个子项目,这些子项目创建使用-flto=thin.

该项目有很多与上述库相关的测试。使用 LTO 需要花费大量时间来构建测试,因此我已禁用 LTO 进行使用-fno-lto.

但我注意到,lld即使使用-fno-lto. 如果我运行链接器,--time-trace我可以看到大部分时间都花在了 LTO 上。

我的问题是:

  1. 这是预期的吗?如果是这样,我可以假设lld只要在它链接的对象中找到 LTO 信息,就会执行 LTO。
  2. 如果没有,有没有办法禁用这种行为?添加-fno-lto到编译器似乎不起作用,并且lld没有显式禁用 LTO 的参数。
  3. 如果不是,这是一个错误吗?

更新1:

这就是我lto在 CMake 中的处理方式:

# Enable Thin LTO only on non-test targets.
if(ENABLE_LTO)
  if (IS_TEST)
    target_compile_options(${TARGET} PRIVATE -fno-lto)
    # Probably pointless.
    target_link_options(${TARGET} PRIVATE -fno-lto)
  else()
    message(STATUS "ENABLE_LTO on target ${TARGET})")
    target_compile_options(${TARGET} PRIVATE -flto=thin)
    target_link_options(${TARGET} PRIVATE -flto=thin -Wl,--thinlto-cache-dir=${CMAKE_BINARY_DIR}/lto.cache)
  endif()
endif()
Run Code Online (Sandbox Code Playgroud)

c++ clang clang++ lto lld

7
推荐指数
1
解决办法
2241
查看次数

C++ constexpr 类型为嵌套类

这有效:(A)

class Foo {
public:
  const bool b;
  constexpr ~Foo() = default;
  constexpr Foo(const bool b) : b(b) {};
};

class Bar {
public:
  static constexpr Foo tru { true };//Foo is complete type
};
Run Code Online (Sandbox Code Playgroud)

编译失败:(B)

class Bar {
public:
  class Foo {
  public:
    const bool b;
    constexpr ~Foo() = default;
    constexpr Foo(const bool b) : b(b) {};
  };
  static constexpr Foo tru { true };//undefined constructor 'Foo' cannot be used
};
Run Code Online (Sandbox Code Playgroud)

错误:

$ clang++ --std=c++20 -D_POSIX_C_SOURCE=200112L -fPIC -g -Werror …
Run Code Online (Sandbox Code Playgroud)

c++ constexpr clang++ c++20

7
推荐指数
1
解决办法
320
查看次数

如果数学移至内联函数,为什么 C++ 舍入行为(对于编译时常量)会发生变化?

考虑以下函数:

static inline float Eps(const float x) {
  const float eps = std::numeric_limits<float>::epsilon();
  return (1.0f + eps) * x - x;
}

float Eps1() {
  return Eps(0xFFFFFFp-24f);
}

float Eps2() {
  const float eps = std::numeric_limits<float>::epsilon();
  const float x = 0xFFFFFFp-24f;
  return (1.0f + eps) * x - x;
}
Run Code Online (Sandbox Code Playgroud)

-O2with中-std=c++20,这两个函数都编译为一个函数,movss后跟一个ret针对 x86 的 using clang 16.0.0 和mov一个bx针对 ARM 的 with gcc 11.2.1。为 ARM 生成的程序集与返回值 ~5.96e-8 一致,但为 x86 生成的程序集则不然。 …

c++ floating-point clang compiler-optimization clang++

7
推荐指数
1
解决办法
257
查看次数

std::span 作为模板模板参数不能用 clang 编译

我想用作std::span函数的模板模板参数。gcc 似乎接受以下代码,但 clang 拒绝。

#include <iostream>
#include <span>
#include <vector>

std::vector v{1,2,3,4,5,6};

template <template <typename> class S>
S<int> GetSpan()
{
    return v;
}

int main()
{
    auto x = GetSpan<std::span>();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么会出现这种情况吗?

https://godbolt.org/z/Ks9M5oqKc

c++ templates clang clang++ c++17

7
推荐指数
2
解决办法
242
查看次数