正如以下代码片段中的注释所说,这是 gcc 4.4 的解决方法。错误,我现在可能应该删除它。有关此背景的信息,请参阅带有 gcc 4.4 的模板模板参数和可变参数模板。
在任何情况下,这都会在带有 clang 3.4.2-4 的 Debian Wheezy 上出现错误,从不稳定向后移植。这适用于 gcc 4.9,也从 Debian Wheezy 上的不稳定(和 4.7)向后移植。
// Workaround for gcc 4.4 bug. See /sf/ask/596024341/
template <typename S, typename T,
template <typename S, typename T, typename... Args> class C,
typename... Args>
struct maptype
{
typedef C<S, T, Args...> type;
};
int main(void){}
Run Code Online (Sandbox Code Playgroud)
错误是
clang++ -o shadow.ocl -c -ftemplate-depth-100 -fno-strict-aliasing -fno-common -ansi -Wextra -Wall -Werror -Wno-unused-function -Wc++0x-compat -Wpointer-arith -Wcast-qual -Wcast-align -std=c++11 -mtune=native -msse3 -O3 shadow.cc
shadow.cc:3:23: …Run Code Online (Sandbox Code Playgroud) 在尝试用 C++ 构建个人项目时,我遇到了一个问题。我不确定它何时出现,因为我通常在 Linux 环境中处理该项目,但希望该项目在 Windows 上运行。
不幸的是,我正在使用的编译器 Clang 已经开始向我抛出以下错误:
libboost_filesystem-vc140-mt-1_61.lib(operations.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in buffer.obj
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何告诉 Clang 底层编译器应该使用哪个运行时,clang-cl如果我不需要,我现在不希望使用。
使 Windows 上的 Clang 链接到动态运行时的正确方法是什么?
考虑以下用于 GNU 扩展的类型特征的小测试程序 __uint128_t
#include <limits>
#include <type_traits>
using uint128_t = __uint128_t;
int main()
{
static_assert(std::is_unsigned<uint128_t>{}, "");
static_assert(std::is_integral<uint128_t>{}, "");
static_assert(std::numeric_limits<uint128_t>::digits == 128, "");
}
Run Code Online (Sandbox Code Playgroud)
这适用于 g++ 和 libstdc++(工作示例)以及 clang++ 和 libc++(工作示例),但不适用于 clang++ 和 libstdc++ 的组合(失败示例)。
请注意,在所有 3 种情况下,我都使用了-std=gnu++1z标志。
问题:哪种命令行参数组合可以使用 libstdc++ 成功编译我的 clang++ 测试程序?
我正在尝试在项目中使用gold链接器。如果我只是这样做clang++CMake
add_definitions(-fuse-ld=gold)
Run Code Online (Sandbox Code Playgroud)
我以以下形式收到大量警告:
clang: warning: argument unused during compilation: '-fuse-ld=gold'
Run Code Online (Sandbox Code Playgroud)
CMake脚本中的哪里添加它以避免到处都有警告?我有一个函数load(std::optional<int> page)可以加载给定页面或所有页面,如果page.empty(). 因为加载是一项代价高昂的操作,所以我缓存了最后加载的页面及其内容。为此,我使用了一个成员变量类型,std::optional<std::optional<int>>它的值应该告诉我当前是否缓存了单个页面、所有页面或根本没有页面。
LLVM 的 libc++ 实现(与 clang 一起提供Apple LLVM version 10.0.0 (clang-1000.11.45.2))在比较 std::optional 实例时有一个令人惊讶的行为,这boost::optional与(用 1.67 测试)不同:
std::cout << (std::optional<int>() == std::optional<std::optional<int>>()); // prints 1
std::cout << (boost::optional<int>() == boost::optional<boost::optional<int>>()); // prints 0
Run Code Online (Sandbox Code Playgroud)
哪个是正确的行为,这是 libc++ 实现中的错误吗?
我买了一台 MacBook Pro,过去两天我一直在使用 MacOS。我一直在尝试编写此 C++ 代码,该代码输出使用chrono和ctime库的日期和时间。这段代码在我的 Windows 机器和 CentOS7 服务器上运行良好。但是,在我的 MacBook Pro 上它无法编译。这是我尝试使用 G++ 编译时收到的错误消息:
main.cpp:19:61: error: no viable conversion from 'time_point<std::__1::chrono::steady_clock,
duration<[...], ratio<[...], 1000000000>>>' to 'const
time_point<std::__1::chrono::system_clock, duration<[...], ratio<[...], 1000000>>>'
std::time_t date = std::chrono::system_clock::to_time_t(now);
^~~
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: candidate constructor
(the implicit copy constructor) not viable: no known conversion from
'std::__1::chrono::time_point<std::__1::chrono::steady_clock, std::__1::chrono::duration<long long,
std::__1::ratio<1, 1000000000> > >' to 'const
std::__1::chrono::time_point<std::__1::chrono::system_clock, std::__1::chrono::duration<long long,
std::__1::ratio<1, 1000000> > > &' for 1st argument
class _LIBCPP_TEMPLATE_VIS time_point
^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/chrono:1340:28: note: …Run Code Online (Sandbox Code Playgroud) 这无法用 clang++ 编译,有人可以解释为什么吗?(这用 g++ 编译得很好)
struct X
{
template <typename T> X() {}
};
template X::X<int>();
int main() { return 1; }
instantiate.cc:7:13: error: qualified reference to 'X' is a constructor name rather than a type in this context
template X::X<int>();
^
instantiate.cc:7:14: error: expected unqualified-id
template X::X<int>();
^
2 errors generated.
Run Code Online (Sandbox Code Playgroud) 我编译了以下代码-Wsign-conversion:
int main()
{
unsigned int a = 8;
int b = a + 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int c = a - 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int d = a * 8u; // warning: implicit conversion changes signedness: 'unsigned int' to 'int'
int e = a / 8u; // gcc warns, but no warning in clang
}
Run Code Online (Sandbox Code Playgroud)
从无符号除法的结果进行分配时,Clang 不会发出警告,但 gcc 会发出警告。
为什么在这种特殊情况下会有所不同?
我有一个程序可以很好地与 GCC 配合使用,但是使用 Clang 编译它反而会使链接器失败。
我认为我的问题是模板类,所以我实现了这个小例子。
test.cpp:
#include "Point.h"
int main()
{
Point<int> p1;
Point<int> p2{ 3, 6 };
}
Run Code Online (Sandbox Code Playgroud)
Point.h:
#ifndef POINT_H
#define POINT_H
template<typename T>
class Point {
T x, y;
public:
Point();
Point(T, T);
};
template class Point<int>;
template class Point<float>;
#endif
Run Code Online (Sandbox Code Playgroud)
Point.cpp:
#include "Point.h"
template<typename T>
Point<T>::Point()
: x{0}, y{0}
{}
template<typename T>
Point<T>::Point(T t_x, T t_y)
: x{t_x}, y{t_y}
{}
Run Code Online (Sandbox Code Playgroud)
当我用 构建它时g++ Point.cpp test.cpp,它可以正常工作。
但是使用 Clang,它会出现以下错误:
$ clang++ Point.cpp test.cpp …Run Code Online (Sandbox Code Playgroud) 根据 cppreference.com ( https://en.cppreference.com/w/cpp/compiler_support#C.2B.2B20_features ) 自版本 8 以来,Clang 部分支持 C++20 协程:
但是如果在 Clang 主干(即将发布的版本 13)中,我写
#include <coroutine>
Run Code Online (Sandbox Code Playgroud)
它导致错误(https://gcc.godbolt.org/z/rTfjbarKz):
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/coroutine:334:2: error: "the coroutine header requires -fcoroutines"
#error "the coroutine header requires -fcoroutines"
Run Code Online (Sandbox Code Playgroud)
如果我-fcoroutines在命令行中添加标志,那么 Clang 会抱怨(https://gcc.godbolt.org/z/qMrv6nMzE):
clang-13: error: unknown argument: '-fcoroutines'
Run Code Online (Sandbox Code Playgroud)
有什么办法可以在 Clang 中开始使用 C++20 协程吗?