我的问题是以下。我想根据ConstexPR值列表对类型进行排序。问题可以归结为此功能:
template <typename U, typename V>
auto min(U,V) -> std::conditional_t<U::value < V::value, U, V>
{ return {}; }
值必须是每种类型的某个静态constexpr。以下片段演示了用法:
// (I)
// This must even be declared outside of a function body due to the statics :(
struct X { static constexpr double value = 2.; };
struct Y { static constexpr double value = 1.; };
int main()
{
X x;
Y y;
auto z = min(x,y);
std::cout << typeid(z).name() << " : " << z.value << std::endl;
}
我的目标是在我调用该功能时提供值。我最接近这个目标的是以下
template <double (*F)()>
struct Value { static constexpr double value = F(); };
可以使用lambdas这样称呼这样的称呼:
// (II)
auto w = min(Value<[]{ return 3.14; }>{}, Value<[]{ return 2.71; }>{});
std::cout << typeid(w).name() << " : " << w.value << std::endl;
实际要排序的类型可以是附加参数。
问题在于,根据标准,上面的C 无效。但是,最新的clang 确实编译这个优雅。
现在,我的问题是:是否有另一种符合标准的方法可以实现上述(列表(ii((,也就是说,定义一个函数根据函数参数(以某种方式(计算基于所提供的constexor对象的类型?
P.S。:我知道使用std::integral_constant
的解决方案。但是,这仅限于整体类型。我对适用于所有constexpr对象的解决方案感兴趣,特别是浮点类型和字符串。
编辑:
要处理浮点值以及可以使用用户定义的文字模板的积分类型的方案,例如:
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <iostream>
template <class FloatingPointType, class... Cs>
constexpr FloatingPointType char_list_to_(Cs... cs) {
char arr[] = {cs...};
FloatingPointType lhs = 0;
bool contains_dot = false;
for (std::size_t i = 0; i < sizeof...(Cs) && !(contains_dot |= (arr[i] == '.')); i++) {
lhs *= 10;
lhs += arr[i] - '0';
}
FloatingPointType rhs = 0;
for (int i = sizeof...(Cs) - 1; i > 0 && arr[i] != '.'; i--) {
rhs /= 10;
rhs += arr[i] - '0';
}
rhs /= 10;
return (contains_dot)?lhs+rhs:lhs;
}
template <class FloatingPointType, char... Cs>
struct FloatingPointValue {
static constexpr FloatingPointType value = char_list_to_<FloatingPointType>(Cs...);
constexpr operator FloatingPointType() {
return value;
}
};
template <class FloatingPointType, char... Cs>
constexpr FloatingPointType FloatingPointValue<FloatingPointType, Cs...>::value;
template <char... Cs>
FloatingPointValue<double, Cs...> operator""_fv() {
return {};
}
template <typename U, typename V>
auto min(U,V) -> std::conditional_t<(U{}<V{}), U, V>
{ return {}; }
int main() {
auto w = min(3.14_fv, 2.71_fv);
std::cout << typeid(w).name() << " : " << w.value << std::endl;
}
输出:
18FloatingPointValueIdJLc50ELc46ELc55ELc49EEE : 2.71
c++filt -t 18FloatingPointValueIdJLc50ELc46ELc55ELc49EEE
的输出:
FloatingPointValue<double, (char)50, (char)46, (char)55, (char)49>
[live demo]
但是,如果您希望将同样的内容应用于字符串,那么当前缺乏由C 标准引起的功能的支持。但是,如果您能够接受较少的便携式选项,则有Clang和GCC支持的GNU扩展名:
#include <type_traits>
#include <utility>
#include <typeinfo>
#include <iostream>
template <class CharT, CharT... Cs>
struct Value {
static constexpr std::size_t size = sizeof...(Cs);
static constexpr CharT const value[sizeof...(Cs) + 1] = {Cs..., ' '};
template <class RHS>
constexpr bool operator<(RHS) {
for (std::size_t i = 0; i < size && i < RHS::size; i++) {
if (value[i] != RHS::value[i]) {
return value[i] < RHS::value[i];
}
}
return size < RHS::size;
}
};
template <class CharT, CharT... Cs>
constexpr CharT const Value<CharT, Cs...>::value[sizeof...(Cs) + 1];
template <class CharT, CharT... Cs>
Value<CharT, Cs...> operator""_v() {
return {};
}
template <typename U, typename V>
auto min(U,V) -> std::conditional_t<(U{}<V{}), U, V>
{ return {}; }
int main() {
auto w = min("cde"_v, "abc"_v);
std::cout << typeid(w).name() << " : " << w.value << std::endl;
}
输出:
5ValueIcJLc97ELc98ELc99EEE : abc
c++filt -t 5ValueIcJLc97ELc98ELc99EEE
的输出:
Value<char, (char)97, (char)98, (char)99>
[live demo]