小编Gus*_*uss的帖子

C++ 重载模式:使用可变 lambda 调用解析

考虑到这个众所周知的 C++ 模式:

template <class... Ts> struct overload : Ts... { using Ts::operator()...; };
template <class... Ts> overload(Ts...) -> overload<Ts...>; // clang needs this deduction guide,
                                                           // even in C++20 for some reasons ...
Run Code Online (Sandbox Code Playgroud)

我想知道为什么将参数声明为可变 lambda 会更改覆盖分辨率。

Godbolt 上的现场示例

#include <iostream>

template <class... Ts> struct overload : Ts... { using Ts::operator()...; };
template <class... Ts> overload(Ts...) -> overload<Ts...>; // clang needs this deduction guide,
                                                           // even in C++20 for some reasons ...

auto main() -> int …
Run Code Online (Sandbox Code Playgroud)

c++ c++17

15
推荐指数
1
解决办法
328
查看次数

P1061“结构化绑定可以引入包”有任何 C++20 替代/技巧吗?

有没有办法将结构化绑定与任意数量的身份一起使用?

P1061“结构化绑定可以引入包”提供了一种方便的解决方案,但目前尚不可用。

我想要实现的是为聚合类型提供类似元组的接口。
std::get<N>(T)std::tuple_element_t<T>, ETC。)。

我已经有一个计算字段的函数,所以我现在正在寻找一种 - 甚至是棘手的方法 - 来实现以下功能:

template <std::size_t N>
constexpr auto as_tuple(auto && value) noexcept {
    auto & [ /* N identities ...*/ ] = value;
    return std::tuple/* of references */{ /* N identities... */ };
}
Run Code Online (Sandbox Code Playgroud)

充满绝望,我使用预处理器尝试了一些想法(免责声明:不是我喜欢的),但没有可扩展的结果。

template <std::size_t N>
constexpr auto as_tuple(auto && value) noexcept {
    auto & [ /* N identities ...*/ ] = value;
    return std::tuple/* of references */{ /* N identities... */ };
} …
Run Code Online (Sandbox Code Playgroud)

c++ c++20

9
推荐指数
1
解决办法
812
查看次数

嵌套模板类型的用户定义推导指南

Q1:命名空间范围内是否允许用户定义的推导指南?

在这里的示例中,GCC 和 Clang 不会产生相同的行为:

#include <tuple>

template <typename T>
struct some_type;
template <template <typename...> typename T, typename ... Ts>
struct some_type<T<Ts...>>
{
    template <typename U>
    class nested
    {
        U member;
    public:
        nested(U &&){}
    };

    // non-namespace scope user-deduction-guide : OK with Clang, fix the deduction issue
    template <typename U>
    nested(U&&) -> nested<U>;
};

void func()
{
    using pack_type = std::tuple<int, char>;
    some_type<pack_type>::nested{
        [](auto &&){}
    };
}
Run Code Online (Sandbox Code Playgroud)

简而言之,我们有一个模板参数类型,嵌套类型本身就是模板参数,模板参数之间没有关系。

template <typename T>
struct some_type;
template <template <typename...> typename T, …
Run Code Online (Sandbox Code Playgroud)

c++ c++17 ctad

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

Clang 错误“未定义模板 std::tuple_size&lt;auto&gt; 的隐式实例化”

对于 Clang 的行为“未定义的模板 smthg<auto>”,是否有一个(干净的)解决方法?

代码示例:

额外的见解:

我期望的是,根据上面提供的示例,分别获得两种具有相同 -可评估- 的类型std::tuple_size_v

  error : cannot mangle this 'auto' type yet
Run Code Online (Sandbox Code Playgroud)

我是否遗漏了一点?

代码示例:

class std::tuple<
   struct std::integral_constant<bool,0>,
   struct std::integral_constant<bool,1>,
   struct std::integral_constant<bool,0>
>
class std::tuple<bool,bool,bool>
Run Code Online (Sandbox Code Playgroud)

c++ clang++ c++20

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

Clang:二进制表达式('const auto' 和'int')的无效操作数,具有依赖于模板上下文的 constexpr 模板变量

Clang error : invalid operands to binary expression ('const auto' and 'int')
Run Code Online (Sandbox Code Playgroud)

语境 :

  • 叮当 12.0.0

简单的问题:

  • 这个错误在这种情况下意味着什么?(例如,这里的消息是否相关?)
  • 为什么这个错误只在用 Clang 编译时发生,而不是 GCC 和 Msvc-cl ?

这是一个最小的复制案例(可在 Godbolt 上获得

template <typename ... Ts>
struct foo
{
    constexpr inline static auto value = 42;
};

template <typename ... Ts>
struct bar
{
    template <typename U>
    constexpr static inline auto foo_value = foo<Ts...>::value; // Error here
    // nb : replacing `auto` with `int` remove the error
};

static_assert(bar<int>::foo_value<int> == …
Run Code Online (Sandbox Code Playgroud)

c++ clang clang++ c++17 c++20

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

C++ 概念中的价值评估

如何正确评估概念声明/requires 子句中的值?

考虑一个概念is_red,检查给定类型是否具有color设置为 的静态 cx 成员/*undefined*/::red,其中枚举中的`/ undefined /;

template <typename T>
concept is_red = requires(T) {
   { T::color == decltype(T::color)::red };
};
Run Code Online (Sandbox Code Playgroud)

这显然是错误的,因为它只检查合成是否定义良好。
因此,这不会按预期工作:

namespace apple {
   enum colors{red, green, yellow };

   struct granny_smith{
      constexpr static auto color = colors::green;
   };
}

static_assert(is_red<apple::granny_smith>); // should fail, but it does not using the previous concept implementation
Run Code Online (Sandbox Code Playgroud)

请参阅此处有关 godbolt 的实例。

这是我目前评估概念价值的方式:

template <bool condition>
using if_t = std::conditional_t<condition, std::true_type, std::false_type>;

template <typename T>
concept is_red …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20

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

标签 统计

c++ ×6

c++20 ×4

c++17 ×3

clang++ ×2

c++-concepts ×1

clang ×1

ctad ×1