我可以实例化一个std :: reference_wrapper< T>其中T是不完全型? [英] Can I instantiate an std::reference_wrapper<T> where T is an incomplete type?

查看:558
本文介绍了我可以实例化一个std :: reference_wrapper< T>其中T是不完全型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

std :: reference_wrapper< T> 允许 T 不完整, code> T& 可以在 T 完成时处理?

Does std::reference_wrapper<T> allow T to be incomplete, in the same way that a T& can be dealt with without T being complete?

GCC 4.9接受以下内容:

GCC 4.9 accepts the following:

#include <functional>

struct woof;

struct test
{
   test(woof& w) : w(w) {}
   std::reference_wrapper<woof> w;
};

struct woof
{
   int a;
};

int main()
{
    woof w;
    test t = w;   // (braced-init would be better, but VS2012!)
}

2012拒绝它与以下消息:

But MSVS 2012 rejects it with the following message:


错误1错误C2139:'woof':未定义的类不允许作为编译器的参数内部类型trait'__is_abstract'c:\program files(x86)\microsoft visual studio 11.0 \vc\include\type_traits 755 1 test3

Error 1 error C2139: 'woof' : an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_abstract' c:\program files (x86)\microsoft visual studio 11.0\vc\include\type_traits 755 1 test3

我怀疑这是因为 op()需要完整类型,但标准不会出现以指定

I suspect this is because the op() needs the full type, but the standard doesn't appear to specify either way.

这些实现中的哪一个遵循标准命令?

Which, if either, of these implementations is following standard mandates?

推荐答案

N3936§ 17.6.4.8其他功能[res.on.functions]


1在某些情况下(替换函数,处理函数,用于实例化标准库模板组件的类型的操作),C ++标准库取决于由C ++程序提供的组件。如果这些组件不满足其要求,则该标准对实施没有要求。

1 In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.

2特别是,在以下情况下,效果未定义:

2 In particular, the effects are undefined in the following cases:


  • ...


  • 除非特别允许该组件。

20.9.3类模板 reference_wrapper [refwrap] 没有显示 reference_wrapper 的这种特定异常,有未定义的行为。两种实现都符合。

A quick scan through 20.9.3 Class template reference_wrapper [refwrap] reveals no such specific exception for reference_wrapper, so your program has undefined behavior. Both implementations are conforming.

这篇关于我可以实例化一个std :: reference_wrapper&lt; T&gt;其中T是不完全型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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