- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试为可以索引的类型编写类型特征,例如std::vector
,它应该包括枚举类型,因为我可以将它们转换为它们的基础类型。
到目前为止,我已经写了以下特征。
#include <iostream>
#include <type_traits>
#include <cinttypes>
#include <vector>
#include <utility>
template<typename T>
struct is_unsigned_integral :
std::integral_constant<
bool,
std::is_integral<T>::value &&
std::is_unsigned<T>::value
> {};
template<typename T>
inline constexpr auto is_unsigned_integral_v =
is_unsigned_integral<T>::value;
template<typename, typename = void>
struct is_index : std::false_type {};
template<typename T>
struct is_index<
T,
std::enable_if_t<
is_unsigned_integral_v<
std::conditional_t<
std::is_enum_v<T>,
std::underlying_type_t<T>,
T
>
>
>
> : std::true_type {};
template<typename T>
inline constexpr auto is_index_v = is_index<T>::value;
enum class idx : unsigned int {};
int main() {
static_assert(is_index_v<unsigned int>, "");
static_assert(is_index_v<idx>, "");
return 0;
}
但我收到以下错误消息
type_traits:2009:15: error:
only enumeration types have underlying types
typedef __underlying_type(_Tp) type;
我希望以下
std::conditional_t<
std::is_enum_v<T>,
std::underlying_type_t<T>,
T
>
评估八至
T
或底层类型是
T
是
enum
.
最佳答案
替换失败,因为没有 std::underlying_type_t<unsigned int>
.
您可以单独专攻:
#include <iostream>
#include <type_traits>
#include <cinttypes>
#include <vector>
#include <utility>
template<typename T>
struct is_unsigned_integral :
std::integral_constant<
bool,
std::is_integral<T>::value &&
std::is_unsigned<T>::value
> {};
template<typename T>
inline constexpr auto is_unsigned_integral_v =
is_unsigned_integral<T>::value;
template<typename, typename = void>
struct is_index : std::false_type {};
template<typename T>
struct is_index<
T,
std::enable_if_t< is_unsigned_integral_v<T> >
> : std::true_type {};
template <typename T>
struct is_index<T,std::enable_if_t<std::is_enum_v<T> >> : is_index<std::underlying_type_t<T> > {};
template<typename T>
inline constexpr auto is_index_v = is_index<T>::value;
enum class idx : unsigned int {};
int main() {
static_assert(is_index_v<unsigned int>, "");
static_assert(is_index_v<idx>, "");
return 0;
}
If T is a complete enumeration (enum) type, provides a member typedef type that names the underlying type of T.
Otherwise, the behavior is undefined. (until C++20)
Otherwise, if T is not an enumeration type, there is no member type. Otherwise (T is an incomplete enumeration type), the program is ill-formed.(since C++20)
std::underlying_type_t<T>
当
T
实际上是一个枚举类型。
关于C++ std::conditional_t 不会用 std::is_enum_v 和 std::underlying_type_t 编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69284927/
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 3 年前。 Improve t
我正在尝试为可以索引的类型编写类型特征,例如std::vector ,它应该包括枚举类型,因为我可以将它们转换为它们的基础类型。 到目前为止,我已经写了以下特征。 #include #include
我尝试执行 examle.cpp项目 OSRM 文件。我做了以下步骤: 从源代码构建 OSRM,如 here 所述 在新终端中:cd osrm-backend/example 在终端:cmake ..
我是一名优秀的程序员,十分优秀!