c++ - 具有未知类型的 std::conditional
问题描述
我目前正在编写一个使用 C++20 的库std::span
。编译器库支持std::span
在这一点上相当稀缺。因此,我有一个允许使用第 3 方实现的 marco(在我的情况下tcb::span
)。宏如下所示:
#ifdef SPAN_BUILTIN
# include <span>
# define SPAN std::span
#else
# include "3rdparty/span.hpp"
# define SPAN tcb::span
#endif
通过库的用法如下所示:
void my_func(const SPAN<int>& s);
这一点都不漂亮。我一直在寻找更好的解决方案,但我遇到了std::conditional
我过去已经使用过的解决方案。一个天真的尝试看起来像这样:
constexpr const bool span_builtin = // ...
template <class T>
using span_type = typename std::conditional<span_builtin, std::span<T>, tcb::span<T>>::type;
这将导致这样的用法:
void my_func(const span_type& s);
问题在于std::span
编译时未知类型std::span
不可用,导致编译失败。
有更好的解决方案吗?
解决方案
好问题!
让我们一步一步来回答
constexpr const bool span_builtin = // ...
这样的事情存在吗?
这行得通吗?
可能不是
https://stackoverflow.com/a/45594334/1691072
我们可以使用它,但问题是在 C++20 以下,Span 不会被定义
我们也不能正式将我们自己的跨度转发声明添加到 std 命名空间
那么解决方案是什么?
解决方案最终将与您的解决方案非常相似
#include <type_traits>
#if __cplusplus > 201703L // Do something else for MSVC if you cannot use `/Zc:__cplusplus`
#include <span>
template<typename T, std::size_t N = std::dynamic_extent>
using span = std::span<T, N>;
#else
template<typename T>
using span = tcb::span<T>;
#endif
int main ()
{
#if __cplusplus > 201703L
static_assert(std::is_same_v< span<int>, std::span<int>>);
#else
static_assert(std::is_same_v< span<int>, tcb::span<int>>);
#endif
}
推荐阅读
- python - 读取和写入 csv 文件
- sql - 如何将天数传递给 Postgres 函数?
- python - 从 Angular 前端对 apache 气流服务器进行 POST api 调用时出现 CORS 问题
- batch-file - 为什么 FindStr 返回 not-found
- visual-studio - Docker 容器中的 Nuget 还原
- amazon-web-services - 如何检索所有安全组 ID 并将它们保存到可迭代列表中?
- java-time - 为什么我的 UTC 时刻没有转换为 BST?
- javascript - Backbone.js 入门定制问题
- amazon-web-services - 如何设置 Cloudwatch SQL 监视器?
- java - 关于将“java”放入android的方法的问题