- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给定以下来自 Anthony Williams 的代码片段.一个非常基本的元组示例,这里的所有内容都按预期工作。
#include <iostream>
template<typename ... Types>
class simple_tuple;
template<>
class simple_tuple<>
{};
template<typename First,typename ... Rest>
class simple_tuple<First,Rest...>:
private simple_tuple<Rest...>
{
First member;
public:
simple_tuple(First const& f,Rest const& ... rest):
simple_tuple<Rest...>(rest...),
member(f)
{}
First const& head() const
{
return member;
}
simple_tuple<Rest...> const& rest() const
{
return *this;
}
};
template<unsigned index,typename ... Types>
struct simple_tuple_entry;
template<typename First,typename ... Types>
struct simple_tuple_entry<0,First,Types...>
{
typedef First const& type;
static type value(simple_tuple<First,Types...> const& tuple)
{
return tuple.head();
}
};
template<unsigned index,typename First,typename ... Types>
struct simple_tuple_entry<index,First,Types...>
{
typedef typename simple_tuple_entry<index-1,Types...>::type type;
static type value(simple_tuple<First,Types...> const& tuple)
{
return simple_tuple_entry<index-1,Types...>::value(tuple.rest());
}
};
template<unsigned index,typename ... Types>
typename simple_tuple_entry<index,Types...>::type
get_tuple_entry(simple_tuple<Types...> const& tuple)
{
std::cout << "SizeofArgs == " << sizeof...(Types) << std::endl;
return simple_tuple_entry<index,Types...>::value(tuple);
}
int main()
{
simple_tuple<int,char,double> st(42,'a',3.141);
std::cout<<get_tuple_entry<0>(st)<<","
<<get_tuple_entry<1>(st)<<","
<<get_tuple_entry<2>(st)<<std::endl;
}
但我想知道 get_tuple_entry
函数。
我原以为可变参数模板参数的数量会因每次调用而异,但 sizeof 始终返回 3。
所以该函数在某种程度上等同于以下(伪代码)
template<unsigned index, <int,char,double> >
typename simple_tuple_entry<index, <int,char,double> >::type
get_tuple_entry(simple_tuple<int,char,double> const& tuple)
{
std::cout << "SizeofArgs == " << sizeof...(<int,char,double>) << std::endl;
return simple_tuple_entry<index,<int,char,double> >::value(tuple);
}
但这意味着 get_tuple_entry
仅由返回值重载,这是不可能的。为什么每次调用的签名都不同?
最佳答案
But this would mean that get_tuple_entry is overloaded only by the return value which is not possible.
get_tuple_entry
不是一个函数,它是一个函数模板。您所说的仅在返回类型上不同的同一函数的三个重载是不相同的。它们是函数模板的不同实例:
get_tuple_entry<0, int, char, double>
get_tuple_entry<1, int, char, double>
get_tuple_entry<2, int, char, double>
这不是同一个函数。
I thought that the number of variadic template parameters would vary for each call, but the sizeof always returns 3
当然。每次调用该函数模板的实例时,您都会传递相同类型的参数 simple_tuple<int,char,double>
, 所以每次推导模板参数包为int, char, double
其大小为 3。调用之间的区别在于您调用了不同的实例化,并且 get_tuple_entry<0>
与 get_tuple_entry<1>
不同,并且每个不同的实例化返回元组的不同元素。
这真的和
#include <iostream>
template<int N>
void func()
{
std::cout << N << '\n';
}
int main()
{
func<0>();
func<1>();
func<2>();
}
这调用了三个不同的函数来打印不同的东西,但是相同的签名没有问题,因为func<0>()
和 func<1>()
和 func<2>()
都是不同的功能。如果您查看损坏的名称,您会发现它们具有不同的签名,例如使用 G++ 我得到 _Z4funcILi0EEvv
和 _Z4funcILi1EEvv
和 _Z4funcILi2EEvv
这不是同一个签名。
关于c++ - 可变参数模板和参数推导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11953204/
运行以下代码片段,我没有收到任何错误,并且得到了预期的结果。但是,由于第二个模板实例化是不明确的 ( both type specifiers are references ),我担心这可能不是定义的
考虑以下示例: #include struct A {}; template void f() { static_assert(std::is_same_v); // #1 A&
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
从 DH 协商中派生的 secret 派生出一个比方说 128 位 AES key 的正确(可接受的)方法是什么? 使用前 128 位 对 secret 进行哈希处理并使用前 128 位 使用一些更复
我对编写模板元编程比较陌生,这可能是我找不到解决这个问题的原因。问题是这样的:我正在开发一个数学库,它有很多函数,比如确定整数或 std::initializer_list 的质数,将整数更改为罗马数
我在 Android Oreo 源代码中阅读了一些我不太理解的代码。 首先,类IOMXNode有一个函数: class IOMXNode : public IInterface { public: +
有很多关于模板参数推导的讨论和澄清,特别是引用折叠和“通用引用”。本题通过相关细节:How does auto deduce type? ,而 Scott Meyers 的这篇论文更详细,可能会提供更
我将 Java 中的许多假设带到了我对 C++ 的学习中,这似乎再次难倒了我。我没有足够的词汇量来 Eloquent 地说出我希望从以下程序中看到什么,所以我只展示它并说出我希望看到的内容: #inc
对于下面的程序,Clang 5 (trunk) 报告 IsNoexcept 不可推导,而 GCC 7.1 会出现段错误。 标准(草案)对此有何评论?这是编译器 QOI 问题吗? static_asse
我最近发现,在 lambda 中按值捕获 const 对象意味着 labmda 主体(即 lambda 的数据成员)内的变量也是 const. 例如: const int x = 0; auto fo
我是否有机会推断出 PHP Closure 参数类型信息?考虑这个例子: 5, 'b' => 10]); } else { call_user_func($closure, 5, 10);
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
对于上述 svm 的拉格朗日函数,我可以得到如下的偏导数: 但是,我不明白如何将它们插入拉格朗日以导出对偶形式? W可以被替换,但是b去哪里了? 有人可以解释一下并给出详细步骤吗? 最佳答案 你的拉格
我正在寻找一些算法、程序或函数来推断变量的创建方式,只要我提供其他变量即可。我认为计算机程序员会称之为“反编译”,而架构师会称之为“逆向工程”,但我想我不知道统计学家会怎么调用它......或者是否有
这就是我的简单类的样子。 template class A { T first; T second; public: A(T f, T s) : first(f), second(s) {}; te
这个问题在这里已经有了答案: Is it possible to figure out the parameter type and return type of a lambda? (5 个答案)
我有一个函数需要两个 std::function s 作为参数。第二个函数的参数与第一个函数的结果类型相同。 我写了一个这样的函数模板: template void examplFunction(st
O'reilly Optimizing SQL Statments Book的Explaining MySQL Explain章节,最后有这个问题。 The following is an examp
举例 template void function(T&& arg) 有人可以详细解释它是如何结束函数签名变成左值的 T& 和传入的右值的 T&& 吗?我知道不知何故(需要标准行)T -> T& 在
我正在开发用于 EMV 交易的软件,但我面临着雇佣我的公司的文档严重缺乏的问题。 其中之一是关于用于生成 ARQC 的 MKD(在第一个 GENERATE AC 期间)。我从消息请求中知道IAD如下:
我是一名优秀的程序员,十分优秀!