c++ - std::get 从模板参数

标签 c++ templates tuples

能否请您解释一下或提供一个链接来解释: 为什么这有效:

template <int i, int j, class T>
auto to_pair(T tuple) -> decltype(std::make_pair(std::get<i>(tuple), std::get<j>(tuple)))
{
    return std::make_pair(std::get<i>(tuple), std::get<j>(tuple));
}

这不是吗?

template <class T>
auto to_pair(int i, int j, T tuple) -> decltype(std::make_pair(std::get<i>(tuple), std::get<j>(tuple)))
{
    return std::make_pair(std::get<i>(tuple), std::get<j>(tuple));
}

我的意思是 int 类型作为模板参数特化和 int 参数作为通用参数有什么区别?

最佳答案

I mean what is the difference between int type being template argument specialization and int argument as a common argument?

一个在编译时定义,另一个在运行时定义。

运行时值不能用作模板参数。因此,第二种情况不起作用。

关于c++ - std::get 从模板参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49415272/

相关文章:

c++ - 如何根据模板参数返回不同的类型

haskell - 如何在没有应用程序的情况下在 Haskell 中实现 uncurry point-free?

python - 如何检查具有列表或字典的元组是否为空

c++ - 我的 IDE 使用什么 C++ 实现/库? (NetBeans、MacOSX)

c++ - OpenCV 3 中的 PCA 错误

c# - Web 浏览器 Hook 和自动化

c++ - 数组语法错误

c++ - 用于高级综合的任意精度浮点库

c++ - 模板化结构内的模板化结构的模板特化失败

swift - “AnyObject”不是 'Custom Tuple' 的子类型