Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C+中元组元素类型的大小之和+;_C++_Templates_C++14_Variadic Templates_Template Meta Programming - Fatal编程技术网

C++ C+中元组元素类型的大小之和+;

C++ C+中元组元素类型的大小之和+;,c++,templates,c++14,variadic-templates,template-meta-programming,C++,Templates,C++14,Variadic Templates,Template Meta Programming,是否可以使用constexpr函数计算std::tuple元素类型的大小之和,用法如下: static_assert(sum_size(std::tuple<int, bool>) == 5, "not 5!"); static_断言(sum_size(std::tuple)==5,“不是5!”); ? 不直接回答我的问题,因为DoSomething不是constexpr函数。我需要在编译时调用DoSomething。或者可能有人可以解释如何将boost::fusion::for

是否可以使用
constexpr
函数计算
std::tuple
元素类型的大小之和,用法如下:

static_assert(sum_size(std::tuple<int, bool>) == 5, "not 5!");
static_断言(sum_size(std::tuple)==5,“不是5!”);
?

不直接回答我的问题,因为DoSomething不是constexpr函数。我需要在编译时调用DoSomething。或者可能有人可以解释如何将
boost::fusion::for_each
static_assert()一起使用

是否可以使用constexpr函数计算具有以下用法的元组元素类型的大小之和:

static_assert(sum_size(std::tuple<int, bool>) == 5, "not 5!");
为什么不呢

不幸的是,您已经标记了C++14

在C++17中更简单(多亏了模板折叠)

模板
constexpr std::size\u t sum\u size(std::tuple const&)
{return(sizeof(Ts)+…);}
在C++14中,您可以编写

template <typename ... Ts>
constexpr std::size_t sum_size (std::tuple<Ts...> const &)
 {
   using unused = std::size_t[];

   std::size_t  ret {};

   (void)unused { 0u, ret += sizeof(Ts)... };

   return ret;
 }
模板
constexpr std::size\u t sum\u size(std::tuple const&)
{
使用unused=std::size_t[];
std::size_t ret{};
(void)未使用的{0u,ret+=sizeof(Ts)…};
返回ret;
}
在C++14(C++11)之前,您必须使用递归,以便。。。可能的解决办法

template <typename = void>
constexpr std::size_t sum_helper ()
 { return 0u; }

template <std::size_t I0, std::size_t ... Is>
constexpr std::size_t sum_helper ()
 { return I0 + sum_helper<Is...>(); }

template <typename ... Ts>
constexpr std::size_t sum_size (std::tuple<Ts...> const &)
 { return sum_helper<sizeof(Ts)...>(); }
模板
constexpr std::size\u t sum\u helper()
{返回0u;}
模板
constexpr std::size\u t sum\u helper()
{返回I0+sum_helper();}
模板
constexpr std::size\u t sum\u size(std::tuple const&)
{返回和_helper();}
但是你必须纠正这个用法

// .........................................VV
static_assert(sum_size(std::tuple<int, bool>{}) == 5, "not 5!");
/..VV
静态断言(和大小(std::tuple{})=5,“不是5!”;

我不确定@Fureeish是否可能重复,至少在那个例子中有些东西不是constexpr。你能解释一下这段代码中到底发生了什么吗?@Fureeish-我希望它是清楚的。。。无论如何:变量包可以在C型数组的初始化列表中展开;因此,它初始化了一个使用过的数组,并且在初始化列表中,它被放置在
ret+=sizeof(Ts)
中,对于每种类型的元组都进行了扩展。所以
ret
累加类型的总和。C++14版本可以工作!但是我的VC2017编译器不理解您的C++17版本。但无论如何,这是一个很好的解决方案。@AlexeyStarinsky-也许VC2017不完全支持C++17;很遗憾,正如您所看到的,C++17版本要简单得多。无论如何为了完整起见,我添加了一个C++11解决方案。@AlexeyStarinsky折叠表达式是在MSVC 15.5中引入的,因此如果您的编译器版本在15.5之前,您需要升级或坚持使用C++14解决方案