考虑到这个众所周知的 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) 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) 在这里的示例中,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) 代码示例:
额外的见解:
error : cannot mangle this 'auto' type yet
Run Code Online (Sandbox Code Playgroud)
我期望的是,根据上面提供的示例,分别获得两种具有相同 -可评估- 的类型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) Clang error : invalid operands to binary expression ('const auto' and 'int')
Run Code Online (Sandbox Code Playgroud)
语境 :
简单的问题:
这是一个最小的复制案例(可在 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) 考虑一个概念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)