问题描述
假设我们创建了两个返回type_of
,like的std::type_identity
函数:
template<auto VAR>
auto type_of() {
return std::type_identity<decltype(VAR)>{};
}
template<typename T>
auto type_of() {
return std::type_identity<T>{};
}
从std::type_identity
获取实际类型的方法似乎有点麻烦:
// this works
// both i1 and i2 are ints
decltype(type_of<int>())::type i1;
decltype(type_of<int{}>())::type i2;
是否有一种方法可以免除上述表达式中对decltype
的需求,或者将其放入可重用的表达式中,从而获得更好的效果:
// can this work?
type_of<int>()::type i1;
type_of<int{}>()::type i2;
甚至更好:
// can this work??
type_of_t<int> i1;
type_of_t<int{}> i2;
注意:类型和非类型模板参数的专门化(可能是一个方向)不起作用(无法编译):
template<auto>
struct type_of;
template<typename T>
struct type_of<T> { // <== compilation error: type/value mismatch
using type = T;
};
template<auto VAR>
struct type_of<VAR> {
using type = decltype(VAR);
};
解决方法
您可以创建类型别名。但是,您不能“超载”它。所以我的解决方案是创建两个:
A <- structure(list(model = c(2,2,6,3,3),term = c("SEX","GAN","GT_rs354","SEX","GT_rs222","GT_rs87623"
)),class = c("spec_tbl_df","tbl_df","tbl","data.frame"),row.names = c(NA,-9L),problems = structure(list(row = 9L,col = "term",expected = "",actual = "embedded null",file = "literal data"),-1L),class = c("tbl_df","data.frame")),spec = structure(list(
cols = list(model = structure(list(),class = c("collector_double","collector")),term = structure(list(),class = c("collector_character","collector"))),default = structure(list(),class = c("collector_guess",skip = 1),class = "col_spec"))
,
在C ++中,模板参数必须是值,类型或其他模板(其自身必须适合声明的模板头)。一定是其中之一。
如果您想这样做:
这个想法是让调用者方不知道模板参数是类型还是变量
能够执行此操作的基本要求是编写一个模板,该模板的参数可以是值或类型。
这不是C ++允许的。
模板函数重载允许您摆脱类似 之类的东西。但这仅能工作,因为它不是一个模板。 两个模板已超载。选择哪个选项取决于提供的模板参数。
模板类不能重载。模板专业化不能更改原始模板的性质(例如其模板参数是什么)。它只能允许您重新解释原始模板参数的模板参数,以提供替代实现。
如果您要这样做,您将不得不等到C ++获得具有可以为任意值的模板参数的能力或C ++获得将类型转换为值并返回的能力(即反射)。
,从std::type_identity
对象can be encapsulated into the following expresion获取类型:
template<auto x>
using type = typename decltype(x)::type;
这将允许替换繁琐的表达式:
decltype(type_of<int>())::type i1;
decltype(type_of<int{}>())::type i2;
使用更简单的表达式:
type<type_of<int>()> i1;
type<type_of<int{}>()> i2;
它仍然需要经历 两步 (先type_of
然后type
),因为第一个步骤必须能够获得 type 或 variable (仅适用于函数模板重载),然后函数无法返回类型,因此它返回需要模板表达式以提取内部类型的对象。
取决于您要使用的类型,代码可以变得更加简单。
如果只想创建该类型的对象,则可以将对象的创建转发到函数中:
template<auto VAR,typename... Args>
auto create_type_of(Args&&... args) {
return decltype(VAR){std::forward<Args>(args)...};
}
template<typename T,typename... Args>
auto create_type_of(Args&&... args) {
return T{std::forward<Args>(args)...};
}
auto i1 = create_type_of<int>(7);
auto i2 = create_type_of<int{}>(7);
以这种方式从std::type_identity
can work创建类型的一般情况:
template<auto VAR>
constexpr auto type_of() {
return std::type_identity<decltype(VAR)>{};
}
template<typename T>
constexpr auto type_of() {
return std::type_identity<T>{};
}
template<typename T,typename... Args>
auto create(std::type_identity<T>,Args&&... args) {
return T{std::forward<Args>(args)...};
}
auto i1 = create(type_of<int>(),7);
auto i2 = create(type_of<int{}>(),7);
请注意,整个操作仅适用于可用作非类型模板参数的变量。有关一种更通用的方法,该方法不需要模板(但带有宏...),因此可以用于不能作为模板参数的变量,请参见this mind blowing neat answer!
,根据设计,C ++模板中的模板参数可以是模板,类型或值。
在模板中,您知道它们是哪个。模板中所有依赖于模板参数的表达式都使用typename
或template
关键字(或上下文)来消除歧义,因此在替换参数之前就知道了它们的类别。
现在有元编程库可以使用所有3个值的替代品。
template<class T> struct type_tag_t {using type=T;};
template<class T> constexpr type_tag_t<T> tag={};
template<template<class...>class Z> struct template_z_t {
template<class...Ts>
using result = Z<Ts...>;
template<class...Ts>
constexpr result<Ts...> operator()( type_tag_t<Ts>... ) const { return {}; }
};
template<template<class...>class Z>
constexpr template_z_t<Z> template_z = {};
此处模板被映射到constexpr函数对象。通过template_z
模板,您可以将类型模板映射到该域。
template<auto x>
using type = typename decltype(x)::type;
template<auto x>
constexpr std::integral_constant<std::decay_t<decltype(x)>,x> k = {};
所以
constexpr auto vector_z = template_z<std::vector>;
constexpr auto vector_tag = vector_z( tag<int>,tag<std::allocator<int>> );
然后您可以返回到类型:
type<vector_tag> v{1,4};
这可能不是您想要的。