gpt4 book ai didi

c++ - 我可以实例化一个 std::reference_wrapper 吗,其中 T 是一个不完整的类型?

转载 作者:可可西里 更新时间:2023-11-01 15:07:26 26 4
gpt4 key购买 nike

是否 std::reference_wrapper<T>允许 T不完整,与 T& 相同可以不用T处理完成了吗?

GCC 4.9 接受以下内容:

#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!)
}

但 MSVS 2012 拒绝它并显示以下消息:

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()需要完整类型,但标准似乎无法指定任何一种方式。

如果有的话,这些实现中的哪一个遵循标准指令?

最佳答案

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

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 In particular, the effects are undefined in the following cases:

  • ...
  • if an incomplete type (3.9) is used as a template argument when instantiating a template component,unless specifically allowed for that component.

快速浏览 20.9.3 类模板 reference_wrapper [refwrap] 显示 reference_wrapper 没有此类特定异常,因此您的程序未定义行为。两种实现都符合要求。

关于c++ - 我可以实例化一个 std::reference_wrapper<T> 吗,其中 T 是一个不完整的类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26043871/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com