首页 > 其他分享 >有意思的 std::disjunction

有意思的 std::disjunction

时间:2024-09-20 12:03:52浏览次数:1  
标签:std 有意思 int assert static template disjunction type

#include <cstdint>
#include <string>
#include <type_traits>
 
// values_equal<a, b, T>::value is true if and only if a == b.
template<auto V1, decltype(V1) V2, typename T>
struct values_equal : std::bool_constant<V1 == V2>
{
    using type = T;
};
 
// default_type<T>::value is always true
template<typename T>
struct default_type : std::true_type
{
    using type = T;
};
 
// Now we can use disjunction like a switch statement:
template<int I>
using int_of_size = typename std::disjunction< //
    values_equal<I, 1, std::int8_t>,           //
    values_equal<I, 2, std::int16_t>,          //
    values_equal<I, 4, std::int32_t>,          //
    values_equal<I, 8, std::int64_t>,          //
    default_type<void>                         // must be last!
    >::type;
 
static_assert(sizeof(int_of_size<1>) == 1);
static_assert(sizeof(int_of_size<2>) == 2);
static_assert(sizeof(int_of_size<4>) == 4);
static_assert(sizeof(int_of_size<8>) == 8);
static_assert(std::is_same_v<int_of_size<13>, void>);
 
// checking if Foo is constructible from double will cause a hard error
struct Foo
{
    template<class T>
    struct sfinae_unfriendly_check { static_assert(!std::is_same_v<T, double>); };
 
    template<class T>
    Foo(T, sfinae_unfriendly_check<T> = {});
};
 
template<class... Ts>
struct first_constructible
{
    template<class T, class...Args>
    struct is_constructible_x : std::is_constructible<T, Args...>
    {
        using type = T;
    };
    struct fallback
    {
        static constexpr bool value = true;
        using type = void; // type to return if nothing is found
    };
 
    template<class... Args>
    using with = typename std::disjunction<is_constructible_x<Ts, Args...>...,
                                           fallback>::type;
};
 

 
int main() {
    // OK, is_constructible<Foo, double> not instantiated
static_assert(std::is_same_v<first_constructible<std::string, int, Foo>::with<double>,
                             int>);
 
static_assert(std::is_same_v<first_constructible<std::string, int>::with<>, std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<const char*>,
                             std::string>);
static_assert(std::is_same_v<first_constructible<std::string, int>::with<void*>, void>);
}

标签:std,有意思,int,assert,static,template,disjunction,type
From: https://www.cnblogs.com/DesertCactus/p/18422219

相关文章

  • C++ std::find函数 容器元素查找
    简介std::find函数是C++标准库内非常实用的一个函数,主要用于在给定范围内查找某个元素,如果找到该元素,则返回指向该元素的迭代器;如果没有找到,则返回指向范围末尾的迭代器(即 end() )。find函数原型std::find在头文件algorithm中template<classInputIt,classT>Inp......
  • std::move()与std::forward()
    在C++中,右值、移动构造函数、std::move()、和std::forward()都是与优化和内存管理相关的概念,特别是在避免不必要的拷贝时有很大作用。1.右值(Rvalue)右值通常是表达式中不具有持久性的临时对象。它是不能通过变量名来引用的值,通常出现在赋值语句的右侧。常见的右值有:字面值:如5......
  • 828 华为云征文|华为 Flexus 云服务器部署 RustDesk Server,打造自己的远程桌面服务器
    ......
  • std::variant快速上手
    std::variant是C++17引入的一种类型安全的联合体,用来存储多个可能类型中的一种值,且保证使用时的类型安全。相比于传统的union,std::variant不仅能够存储不同类型的值,还能自动管理复杂类型的构造与析构。接下来,介绍如何快速上手std::variant。1.定义std::variant使用std:......
  • 【CMake】使用CMake在Visual Stdudio中配置glad和glfw
    下载glad和glfwgladgladglad下载:glad下载这个是......
  • 【CMake】使用CMake在Visual Stdudio编译资源文件和多目标编译
    一、资源文件的编译首先,我们的项目结构如下,存在图片和第三方库:配置主CMakel......
  • C++11 线程同步接口std::condition_variable和std::future的简单使用sk
    合集-C++(1)1.C++11线程同步接口std::condition_variable和std::future的简单使用09-17收起std::condition_variable条件变量std::condition_variable有wait和notify接口用于线程间的同步。如下图所示,Thread2阻塞在wait接口,Thread1通过notify接口通知Thread2继续执行。......
  • C++11 线程同步接口std::condition_variable和std::future的简单使用
    std::condition_variable条件变量std::condition_variable有wait和notify接口用于线程间的同步。如下图所示,Thread2阻塞在wait接口,Thread1通过notify接口通知Thread2继续执行。具体参见示例代码:#include<iostream>#include<mutex>#include<thread>#include<queue>std......
  • Docker-Compose搭建RustDesk服务器
    前置条件:电脑安装RustDesk客户端,服务器安装Docker及docker-compose官方文档:安装::RustDesk文档操作流程:使用Vim编写docker-compose.yml文件,修改需要的端口,最好按照官方对应的端口来操作,<>内替换成服务器对外的端口。记住挂载文件路径,容器运行后会生成密钥保存在挂载......
  • fastDFS - 单机部署 + nginx
    准备查看操作系统的版本信息[root@lab10~]#cat/etc/redhat-releaseCentOSLinuxrelease7.9.2009(Core)查看操作系统的网卡地址[root@lab10~]#ipaddressshowens322:ens32:<BROADCAST,MULTICAST,UP,LOWER_UP>mtu1500qdiscpfifo_faststateUPgroupdef......