相关疑难解决方法(0)

使用可变参数模板进行扩展

以下3个gun函数调用有什么区别?

template <class... Ts> void fun(Ts... vs) {
  gun(A<Ts...>::hun(vs)...);
  gun(A<Ts...>::hun(vs...));
  gun(A<Ts>::hun(vs)...);
}
Run Code Online (Sandbox Code Playgroud)

我对使用特定示例解释三个调用的答案感兴趣.

c++ templates variadic-templates c++11

36
推荐指数
2
解决办法
1835
查看次数

将std :: tuple转换为std :: array C++ 11

如果我有std::tuple<double, double, double>(类型是同类的),是否有转换为的股票函数或构造函数std::array<double>

编辑::我能够使用递归模板代码(我在下面发布的草稿答案).这是处理这个问题的最佳方法吗?这似乎会有一个库存函数......或者如果你对我的答案有所改进,我会很感激.我会留下未回答的问题(毕竟,我想要一个方法,而不仅仅是一种可行的方式),并且更愿意选择别人的[希望更好]答案.

谢谢你的建议.

c++ arrays tuples c++11

18
推荐指数
4
解决办法
7044
查看次数

可以使用模板/预处理器hackery来支持参数列表中间的变量参数吗?

我偶然发现了看起来像这样的旧代码:

void dothing(bool testBool,
               const std::string& testString1,
               const std::string& file,
               int line,
               const std::string& defaultString = "")
{
     // do something...
}

void dothings(bool testBool,
               const std::string& testString1,
               const std::string& testString2,
               const std::string& file,
               int line,
               const std::string& defaultString = "")
{
    dothing(testBool, testString1, file, line, defaultString);
    dothing(testBool, testString2, file, line, defaultString);
}

void dothings(bool testBool,
               const std::string& testString1,
               const std::string& testString2,
               const std::string& testString3,
               const std::string& file,
               int line,
               const std::string& defaultString = "")
{
   dothings(testBool, testString1, testString2, file, …
Run Code Online (Sandbox Code Playgroud)

c++ macros templates c++11

7
推荐指数
1
解决办法
226
查看次数

如何构造一个填充了一些统一值的 std::array ?

std::array 可以使用特定值构造(在编译时使用较新的 C++ 版本),例如

std::array a{1, 4, 9};
Run Code Online (Sandbox Code Playgroud)

但是 - 它没有构造函数,或命名为构造函数习语的标准库,采用单个值并复制它。即我们没有:

std::array<int, 3> a{11};
// a == std::array<int, 3>{11, 11, 11};
Run Code Online (Sandbox Code Playgroud)

因此,我们如何构造一个仅给定重复值的数组?

编辑:我正在寻找一种解决方案,它甚至适用于不可默认构造的元素类型;因此,通过默认构造数组然后填充它的解决方案不是我所追求的 - 尽管事实上这适用于int(如示例中所示)的情况。

c++ arrays stdarray

6
推荐指数
1
解决办法
207
查看次数

如何获取参数包中的元素索引

如何获取以下内容以将参数包元素的索引放入元组中?

template< typename... Ts >
class ClassA {
public:
    ClassA( Ts... ts ) : tup( make_tuple( ts, 0 )... ) {}
    // I would like it to expand to this.
    // ClassA( T0 ts0, T1 ts1 ) : tup( make_tuple( ts0, 0 ), make_tuple(ts1, 1) ) {}
    tuple<tuple<Ts, size_t>...> tup;
};

void main() {
    vector<int> a ={ 2, 4, 5 };
    list<double> b ={ 1.1, 2.2, 3.3 };
    ClassA<vector<int>, list<double>, vector<int>, list<double>> mm( a, b, a, b );
}
Run Code Online (Sandbox Code Playgroud)

谢谢.

c++ templates template-meta-programming variadic-templates c++17

3
推荐指数
1
解决办法
876
查看次数