取决于模板参数的条件存储类

问题描述 投票:0回答:1

以下内容似乎不是有效的c ++(-std=c++2a)。我想基于模板参数var更改b的存储类:

#include <type_traits>

template <bool b>
void f() {
  typename std::conditional<b, static int, int>::type var;
}
c++ typetraits storage-class-specifier
1个回答
0
投票

您可以提供f的完整专业知识,例如:

template <bool b> void f();

template <> void f<true>()
{
  int var;
}

template <> void f<false>()
{
  static int var;
}

这应该与您要实现的效果相同。

© www.soinside.com 2019 - 2024. All rights reserved.