应该std ::函数赋值忽略返回类型吗? [英] Should std::function assignment ignore return type?

查看:118
本文介绍了应该std ::函数赋值忽略返回类型吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据C ++ 11或C ++ 14标准,代码是否有效C ++?

Is the code below valid C++ according to the C++11 or C++14 standard?

#include <functional>

int ReturnInt()
{
  return 5;
}

int main( int argc, char **argv )
{
  std::function< void () > BoundType = ReturnInt;
  return 0;
}

代码使用最新的cygwin版本的gcc(4.8.3)和clang(4.3.2),但不与Visual Studio 2013,Visual Studio 2013年11月CTP或Visual Studio 14预览。如果std :: function更改为boost :: function,它还会在所有平台上编译。

The code compiles fine with the latest cygwin versions of gcc (4.8.3) and clang (4.3.2) but not with Visual Studio 2013, Visual Studio November 2013 CTP or the Visual Studio 14 preview. It also compiles on all platforms if std::function is changed to boost::function.

我发现这个其他堆栈溢出问题,建议它应该工作。

I found this other stack overflow question that suggests it should work.

推荐答案

代码在C ++ 11中是未定义的行为,在C ++ 14中是不成形的。 C ++ 14将此注释添加到此构造函数的规范中:

The code is undefined behavior in C++11, and ill-formed in C++14. C++14 adds this Remark to the specification of this constructor:


注释:这些构造函数不应该参与重载
解析,除非 f 对于参数类型是Callable(20.9.11.2)
ArgTypes ... 并返回类型 R

Remarks: These constructors shall not participate in overload resolution unless f is Callable (20.9.11.2) for argument types ArgTypes... and return type R.

Callable在[func.wrap.func] / p2中定义:

Callable is defined in [func.wrap.func]/p2:


可调用对象 f ArgTypes
类型的类型 F 并且如果表达式 INVOKE (f,declat ...),则返回类型 R / code>,被认为是一个未评估的操作数
(第5条),形成良好(20.9.2)。

A callable object f of type F is Callable for argument types ArgTypes and return type R if the expression INVOKE (f, declval<ArgTypes>()..., R), considered as an unevaluated operand (Clause 5), is well formed (20.9.2).

为了使这个 INVOKE 形成良好,不带 R INVOKE 的返回类型必须是隐式转换到 R ([func.require] / p2)。

For this INVOKE to be well formed, the return type of INVOKE without the R must be implicitly convertible to R ([func.require]/p2).

在C ++ 11中, 子句,这意味着由客户端决定是否正确,如果客户端失败,任何情况都会发生,包括成功的编译。

In C++11 these statements were under a Requries clause, which means it is up to the client to get them right, and if the client fails, anything can happen, including successful compilation.

这已由 LWG 2132 更改。

这篇关于应该std ::函数赋值忽略返回类型吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆