问题标签 [type-deduction]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
625 浏览

c++ - 自动类型推断和 auto&& vs auto

我刚刚看了C++11 中的Scott Meyers通用引用,有一件事情我不太明白。

我对auto作为“通用参考”auto&&的 a 和常规的 a 之间有什么区别感到有些困惑,auto它们什么时候不同?

0 投票
1 回答
23526 浏览

c++ - 删除 decltype 中的引用(返回 T 而不是 T&,其中 T& 是 decltype)

(如果您是 C++11 专业人士,请跳至粗体段落。)

假设我想编写一个模板方法,该方法调用并返回传递的对象的结果,该对象的类型是模板参数:

所以T必须有一个方法ReturnType T::bar() const才能在这样的调用中使用:

由于类型推导,我们不必写MyClass感谢,调用变为:

但是省略<int>也会导致编译错误,因为该方法不需要返回 int 以便x之后分配(例如,它可以返回char)。

在 C++0x/11 中,我们可以使用autoand来推断模板方法的返回类型:decltype

编译器现在将找出类型foo.bar()是什么,并将其用作返回类型。对于我们的具体类MyClass,这将是一个int,以下就足够了:

现在我的问题:

如果 MyClass 定义bar()为返回一个int&,则返回类型doSomething(object)也将是一个int&= decltype(foo.bar())。这是一个问题,因为 G++ 现在符合我正在返回对临时的引用

我怎样才能解决这个问题?有没有remove_reference可以像这样使用的东西remove_reference(decltype(foo.bar()))

我想只声明一个辅助方法,它接受 aT&并返回 aT然后定义返回类型doSomethingto decltype(helper(foo.bar()))。但必须有更好的方法,我感觉到了。

0 投票
2 回答
974 浏览

list - 无法推断(a ~ [a])

我尝试编写一个函数,它接受一个子列表列表,反转子列表并返回串联的反转子列表。这是我的尝试:

我收到此错误:

我究竟做错了什么?第二个问题是 - 类型签名可以更通用吗?我必须写得尽可能通用。

0 投票
2 回答
115 浏览

c++ - 为什么类型扣除不能按预期工作?

我有一个关于 C++ 元编程中的类型推导的小问题。有一定的功能做一些动作。

主文件

输出:

为什么函数foo()中的arg具有与函数main()中的数组不同的数据类型?

0 投票
2 回答
1066 浏览

c++ - Why does iterator type deduction fail?

Why does this not work in C++?
Why can't I restrict foo's parameter to std::vector<T>::iterator like this, and what is the best workaround?

The error is:

0 投票
1 回答
1631 浏览

c++ - 从模板基类派生时找不到类型

我很难理解为什么以下两段代码存在差异,编译器到底在做什么。

我有以下一些琐碎的代码,可以按预期编译没有任何问题:

我从上面获取代码并添加一些模板参数,并开始收到与 booboo 类型无效相关的错误:

错误:

http://ideone.com/jGKYIC

.

我想详细了解,典型的 c++ 编译器如何编译代码的模板版本,它与编译原始示例有何不同,这是与代码的多次传递有关的问题,以及类型相关的外观 - UPS?

0 投票
2 回答
3774 浏览

c++ - `new auto` 有什么作用?

当我使用它是什么意思new auto?考虑表达式:

动态分配对象的类型是什么?它返回的指针的类型是什么?

0 投票
2 回答
179 浏览

c++ - 在构造过程中是否有一种简洁的方法来导出成员的类型?

我有:

  • unarchive接受字典和键并基于传递的模板类型 ( )的模板例程T可以专门用于生成T
  • a 的构造函数,用于struct构造unarchive其成员

一个例子可能如下:

在这里使用的好处unarchive<decltype(value_m)>是我可以改变类型而value_m不用更新这行代码——类型总是跟随成员变量的类型。

我遇到的问题更美观:它非常冗长。目前我有一个宏:

foo的构造函数变化如下:

现在我有一个更简洁但更丑陋的结果。没有宏能达到同样的效果吗?我想要的是类似于:

如何才能做到这一点?

0 投票
3 回答
1724 浏览

c++ - Type deduction fails with pointer to member method

I have the following template class which acts as a proxy. It has a method named call which is supposed to be used to call methods on the wrapped object. There's a problem with it. The type deduction fails and I cannot understand why.

Hudsucker::f takes an std::string and then no matter if I pass an std::string or a const reference to it the compiler is able to call the right method.

But in case of Hudsucker::g with takes a const reference to std::string type deduction fails in both cases with both GCC and Clang.

GCC error for the first line:

Especially this bit is strange: no matching function for call to Proxy<Hudsucker>::call(void (Hudsucker::*)(const string&), const string&). That is exactly the signature I would expect to see work.

Clang error for the first line:

Code:

Could you explain why the type deduction fails in that way? Is there a way to get this to compile with const references?

0 投票
3 回答
1877 浏览

c++ - 使用模板模板参数时模板参数推导失败

std::vector<T>我想创建一个简单的辅助算法,它可以用几何级数填充容器,例如(第一项是a,第一项na * pow(r, n-1),其中r是给定的比率);我创建了以下代码:

在尝试编译时会产生以下错误:

Clang 的错误信息更加微妙:

我原以为使用模板模板参数我不仅可以推断出容器,还可以推断出容器的value_typeT在这种情况下)。

所以,问题是:如何创建一个能够同时推断容器类型和值类型的通用函数?

我确定我遗漏了一些明显的东西 - 感谢您的耐心和帮助。

编辑(答案)

以下代码按预期运行:

输出: