如何创建一个返回类型的constexpr函数(在模板参数中使用)

And*_*ner 14 c++

我正在寻找一些基于模板参数编号创建具有模板参数类型的类的方法.

我想要做的是这样的事情:

template<size_t n>
constexpr auto type_from_size() {
    if(n < 256) {
        return uint8_t;
    } else {
        return uint16_t;
    }
}

template<size_t n>
class X {
    type_from_size<n>() t;
}

X<500> x;
x.t = 500;
Run Code Online (Sandbox Code Playgroud)

因此,在上面的代码中,constexpr函数type_from_size()将接收数字500并返回类型uint16_t,这将是成员的类型X.t.

我知道这显然是可怕的代码,但是这可能使用模板吗?

ken*_*ytm 19

函数无法返回类型.你应该使用一个模板.

仅在两种类型之间进行选择,内置std::conditional就足够了.

#include <type_traits>
#include <cstdint>

template <size_t n>
using type_from_size = typename std::conditional<(n < 256), uint8_t, uint16_t>::type;
// ^ if `n < 256`, the ::type member will be typedef'ed to `uint8_t`.
//                 otherwise, it will alias to `uint16_t`.
//   we then give a convenient name to it with `using`.

template <size_t n>
struct X {
    type_from_size<n> t;
    // ^ use the template
};
Run Code Online (Sandbox Code Playgroud)

如果您需要支持两个以上的值,您可以conditionalif/else if/else链一样改变多个值,但是OH MY EYES

template <size_t n>
using type_from_size =
    typename std::conditional<(n <= 0xff), uint8_t,
        typename std::conditional<(n <= 0xffff), uint16_t,
            typename std::conditional<(n <= 0xffffffff), uint32_t,
                uint64_t
            >::type
        >::type
    >::type;
Run Code Online (Sandbox Code Playgroud)

你也可以和std::enable_if(SFINAE)一起使用专业化来使它更"低级":

template <size_t n, typename = void>
struct type_from_size_impl;
// Declare a "size_t -> type" function.
//  - the `size_t n` is the input
//  - the `typename = void` is a placeholder
//    allowing us to insert the `std::enable_if` condition.

template <size_t n>
struct type_from_size_impl<n, typename std::enable_if<(n <= 0xff)>::type> {
    using type = uint8_t;
};
// We add a partial specialization
//  - in `std::enable_if<c>::type`, if `c` is true, `::type` will be typedef'ed to `void`
//  - otherwise, `::type` will not be defined.
//  - if `::type` is not defined, substitution failed,
//    meaning we will not select this specialization

template <size_t n>
struct type_from_size_impl<n, typename std::enable_if<(n > 0xff && n <= 0xffff)>::type> {
    using type = uint16_t;
};

template <size_t n>
struct type_from_size_impl<n, typename std::enable_if<(n > 0xffff && n <= 0xffffffff)>::type> {
    using type = uint32_t;
};

template <size_t n>
struct type_from_size_impl<n, typename std::enable_if<(n > 0xffffffff)>::type> {
    using type = uint64_t;
};

template <size_t n>
using type_from_size = typename type_from_size_impl<n>::type;
// Here we want to find a specialization of `type_from_size_impl<n>`
// All 4 specializations will be tried.
// If only one specialization works, we will use that one
// (Which is why we need to ensure the ranges are not overlapping
//  otherwise the compiler will complain)
// Then we take the `::type` out the complete this "type-level function".
Run Code Online (Sandbox Code Playgroud)


Asy*_*ode 7

constexpr函数不能直接返回类型,但一个简单的包装器就可以了。

template <typename T>
struct TypeWrapper {
    using type = T;
};

template <size_t n>
constexpr auto type_from_size_func() {
    if constexpr (n <= 0xff) {
        return TypeWrapper<uint8_t>{};
    } else {
        if constexpr (n <= 0xffff) {
            return TypeWrapper<uint16_t>{};
        } else {
            return TypeWrapper<uint32_t>{};
        }
    }
}

template <size_t N>
using type_from_size = typename decltype(type_from_size_func<N>())::type;
Run Code Online (Sandbox Code Playgroud)

用法

type_from_size<123> x; /*uint8_t*/
type_from_size<1234> y; /*uint16_t*/
type_from_size<12345678> z; /*uint32_t*/
Run Code Online (Sandbox Code Playgroud)


Bar*_*rry 5

让我们过度杀戮.从选择器开始:

template <int I> struct choice : choice<I + 1> { };
template <> struct choice<10> { };

struct otherwise { otherwise(...) { } };
Run Code Online (Sandbox Code Playgroud)

然后创建一个返回类型的级联系列重载.选择确保首先选择最小类型,而不必为所有中间整数类型写入双边范围:

template <class T> struct tag { using type = T; }
template <size_t N> using size_t_ = std::integral_constant<size_t, N>;

template <size_t N, class = std::enable_if_t<(N < (1ULL << 8))>>
constexpr tag<uint8_t> tag_from_size(size_t_<N>, choice<0> ) { return {}; }

template <size_t N, class = std::enable_if_t<(N < (1ULL << 16))>>
constexpr tag<uint16_t> tag_from_size(size_t_<N>, choice<1> ) { return {}; 

template <size_t N, class = std::enable_if_t<(N < (1ULL << 32))>>
constexpr tag<uint32_t> tag_from_size(size_t_<N>, choice<2> ) { return {}; }

template <size_t N>
constexpr tag<uint64_t> tag_from_size(size_t_<N>, otherwise) { return {}; }
Run Code Online (Sandbox Code Playgroud)

然后你可以编写调度的顶级:

template <size_t N>
using type_from_size_t = typename decltype(tag_from_size(size_t_<N>{}, choice<0>{}))::type;
Run Code Online (Sandbox Code Playgroud)

并使用它:

template <size_t N>
class X {
    type_from_size_t<N> t;
};
Run Code Online (Sandbox Code Playgroud)


Que*_*tin 5

当然.这是一种更灵活的方式,您可以根据需要添加任意数量的范围,只要它们不重叠即可.

template <std::size_t N, class = void>
struct TypeForSize_;

template <std::size_t N>
struct TypeForSize_<N, std::enable_if_t<
    (N <= 255)
>> { using type = std::uint8_t; };

template <std::size_t N>
struct TypeForSize_<N, std::enable_if_t<
    (N > 255 && N <= 65535)
>> { using type = std::uint16_t; };

template <std::size_t N>
using TypeForSize = typename TypeForSize_<N>::type;
Run Code Online (Sandbox Code Playgroud)

使用未定义类型的大小将导致编译时错误.