我正在尝试学习如何使用 SFINAE。出于练习目的,我试图制作一个std::ostream包装器以制作自定义格式化程序。
这是我的 SFINAE 和自定义输出类。
// Tester
template <class O>
struct is_ostreamable {
template <class T>
static auto check(T t) -> decltype(std::declval<std::ostream &>() << t, std::true_type());
template <class>
static auto check(...) -> std::false_type;
public:
static constexpr bool value{std::is_same_v<decltype(check<O>(0)), std::true_type>};
};
// Custom class
struct CustomOutput {
// Constructor etc...
CustomOutput(std::ostream &os = std::cout) : os{os} {}
std::ostream &os;
// Problematic template function
template <class O, class = std::enable_if_t<is_ostreamable<O>::value>>
CustomOutput &operator<<(O o) {
os << o;
return *this;
} …Run Code Online (Sandbox Code Playgroud) 我刚刚将编译器升级到 GCC 11.1.0,但在尝试编译使用 LLVM 的程序时遇到了麻烦。
\n发生的错误如下:
\nIn file included from /usr/include/llvm/IR/DebugInfo.h:19,\n from /home/<redacted>/src/sword/format/llvm.hpp:12,\n from /home/<redacted>/src/main.cpp:9:\n/usr/include/llvm/ADT/STLExtras.h:1793:18: error: expected unqualified-id before \xe2\x80\x98const\xe2\x80\x99\n 1793 | result_pair<R>(const result_pair<R> &Other)\n | ^~~~~\n/usr/include/llvm/ADT/STLExtras.h:1793:18: error: expected \xe2\x80\x98)\xe2\x80\x99 before \xe2\x80\x98const\xe2\x80\x99\n 1793 | result_pair<R>(const result_pair<R> &Other)\n | ~^~~~~\n | )\n/usr/include/llvm/ADT/STLExtras.h:1843:22: error: expected unqualified-id before \xe2\x80\x98const\xe2\x80\x99\n 1843 | enumerator_iter<R>(const enumerator_iter<R> &Other) : Result(Other.Result) {}\n | ^~~~~\n/usr/include/llvm/ADT/STLExtras.h:1843:22: error: expected \xe2\x80\x98)\xe2\x80\x99 before \xe2\x80\x98const\xe2\x80\x99\n 1843 | enumerator_iter<R>(const enumerator_iter<R> &Other) : Result(Other.Result) {}\n | ~^~~~~\n | )\n\nRun Code Online (Sandbox Code Playgroud)\n我查看了源代码,但不确定 LLVM 的代码是否格式错误,或者是 GCC …