首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >支持动态选择的C++模板类型

支持动态选择的C++模板类型
EN

Stack Overflow用户
提问于 2018-06-08 03:20:38
回答 2查看 310关注 0票数 0

我正在寻找一种结构,它允许编译器为模板参数中的给定整数选择最小数据类型。我自己找不到解决方案的问题是我想如何使用它:

代码语言:javascript
复制
template<typename T, min_type<max_elements_to_store> max_elements_to_store>
class testClass {
private:
    T data[max_elements_to_store];
    min_type<max_elements_to_store> currently_selected_element;
};

宏"min_type“应该为给定的max_elements_to_store (uint8_t、uint16_t、uint32_t、uint64_t)动态选择位数最少的类型。我可以通过简单地将min_type<>替换为给定的数据类型来修复它,但是这种数据类型通常不是最佳的选择。例如,

代码语言:javascript
复制
template<typename T, uint64_t max_elements_to_store>
class testClass {
private:
    T data[max_elements_to_store];
    uint64_t currently_selected_element;
};

TestClass<uint8_t, 12> testObject;

在这里,数组只能容纳12个元素,currently_selected_element变量浪费了很多位,而这些位对于只访问12个元素来说根本不是必需的。这似乎只是一个小问题,但对于访问类中数据的许多变量来说,这会变得更糟……

这个问题有解决方案吗?我希望我要找的东西很清楚。

提前感谢!激发灵感

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-08 03:34:37

其中一种方法是使用std::conditional

代码语言:javascript
复制
#include <type_traits>
#include <cstdint>
#include <limits>

template<std::size_t Count>
using min_type = std::conditional_t<Count <= std::numeric_limits<uint8_t>::max(), uint8_t,
                    std::conditional_t<Count <= std::numeric_limits<uint16_t>::max(), uint16_t,
                        std::conditional_t<Count <= std::numeric_limits<uint32_t>::max(), uint32_t,
                            std::conditional_t<Count <= std::numeric_limits<uint64_t>::max(), uint64_t, void>>>>;

// test: all of the following pass            
static_assert(std::is_same_v<min_type<12>, uint8_t>);
static_assert(std::is_same_v<min_type<1024>, uint16_t>);
static_assert(std::is_same_v<min_type<70000>, uint32_t>);
static_assert(std::is_same_v<min_type<5000000000>, uint64_t>);

template<typename T, std::size_t max_elements_to_store>
class testClass {
private:
    T data[max_elements_to_store];
    min_type<max_elements_to_store> currently_selected_element;
};

TestClass<uint8_t, 12> testObject;

注意:正如@François Andrieux在评论中指出的那样,由于填充,这不会节省任何内存(除非您使用编译器特定的扩展来强制压缩类的表示形式)。

票数 2
EN

Stack Overflow用户

发布于 2018-06-08 04:28:58

下面是另一种解决方案,使用常量表达式函数来索引类型选项的元组。

代码语言:javascript
复制
#include <iostream>
#include <tuple>

template <size_t size>
struct min_type {

    constexpr static inline size_t log2(size_t n)  {
        return ( (n<2) ? 0 : 1+log2(n/2));
    }

    constexpr static inline size_t get_index(size_t last,size_t value) {
        return value == 8 ?  last : get_index(last+1,value/2);
    }

    constexpr static inline std::size_t min_type_size(std::size_t v) {
        v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; 
        v |= v >> 8; v |= v >> 16; v++;
        return v < 8 ? 8 : v;
    }

    using type_options = std::tuple<uint8_t,uint16_t,uint32_t,uint64_t>;

    using type = typename std::tuple_element<
        get_index(0,min_type_size(log2(size))),
        type_options>::type;
};

template<typename T, size_t max_elements_to_store>
class testClass {
//private:
  public:
    T data[max_elements_to_store];
    typename min_type<max_elements_to_store>::type currently_selected_element;
};

int main()
{
    min_type<0xff>::type uint_8_value;
    min_type<0xffff>::type uint_16_value;
    min_type<0xffffffff>::type uint_32_value;
    min_type<0xffffffffffffffff>::type uint_64_value;

    static_assert(std::is_same< decltype(uint_8_value),uint8_t>::value,"...");
    static_assert(std::is_same< decltype(uint_16_value),uint16_t>::value,"...");
    static_assert(std::is_same< decltype(uint_32_value),uint32_t>::value,"...");
    static_assert(std::is_same< decltype(uint_64_value),uint64_t>::value,"...");

    testClass<int,12> testcls;

    static_assert(std::is_same< decltype(testcls.currently_selected_element),uint8_t>::value,"...");

    return 0;
}

Demo

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50748761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档