小编fun*_*run的帖子

C++ 表达式 SFINAE 和 ostream 操纵器

我正在尝试学习如何使用 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)

c++ templates sfinae c++17

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

LLVM STLExtras 中的错误:GCC11 的“const”之前预期有不合格的 id

我刚刚将编译器升级到 GCC 11.1.0,但在尝试编译使用 LLVM 的程序时遇到了麻烦。

\n

发生的错误如下:

\n
In 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\n
Run Code Online (Sandbox Code Playgroud)\n

我查看了源代码,但不确定 LLVM 的代码是否格式错误,或者是 GCC …

c++ gcc constructor class llvm

0
推荐指数
1
解决办法
913
查看次数

标签 统计

c++ ×2

c++17 ×1

class ×1

constructor ×1

gcc ×1

llvm ×1

sfinae ×1

templates ×1