地图不完整的值类型 [英] map with incomplete value type

查看:222
本文介绍了地图不完整的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

  class Test 
{
std: :地图<的std :: string,试验>测试;
};

错误是Field has incomplete type'Test'。我读了几个线程,建议这可能是libcxx版本的xcode附带的错误,但是如果我只需要将其更改为:

  class Test 
{
std :: map< std :: string,std :: shared_ptr< Test>>测试;
};

我只是想检查一下,这绝对是一个正确的错误,而不是一个错误。 >

Cheers!

解决方案

该标准要求,模板组件中使用的所有类型的标准库必须完整,除非另有说明。因此libc ++的错误是正确的。使用不完整的类型是未定义的行为(§17.6.4.8[res.on.functions] / 2),所以libstdc ++接受它也没有错。



您可以使用指针对象来构建完整的类型。但您也可以使用一些第三方集合库,这些库允许不完整的类型,例如 boost :: container: :地图 设计为允许不完整的类型






§17.6.4.8其他功能/ 2:


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




  • ...


  • 如果在实例化模板组件时使用不完整的类型(3.9)作为模板参数,除非特别允许组件。



(明确允许不完整类型的唯一组件是 declare unique_ptr default_delete shared_ptr weak_ptr enable_shared_from_this 。)


I'm getting an error with the following:

class Test
{
    std::map<std::string,Test> test;
};

The error is "Field has incomplete type 'Test'". I read a few threads with suggested this might be a bug in the version of libcxx which ships with xcode, but it wouldn't surprise me at all if I just have to change it to:

class Test
{
    std::map<std::string,std::shared_ptr<Test>> test;
};

I just wanted to double check that this is definitely a correct error and not a bug.

Cheers!

解决方案

The standard requires that, all types used in template components of the standard library must be complete, unless otherwise stated. Thus libc++'s error is correct. Using an incomplete type is undefined behavior (§17.6.4.8[res.on.functions]/2), so libstdc++ accepting it isn't wrong either.

You could use a pointer object to construct the complete type as you did. But you could also use some third-party collections library which allows incomplete types, e.g. boost::container::map is designed to allow incomplete types.


§17.6.4.8 Other functions / 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.

(The only components that explicitly allow incomplete types are declval, unique_ptr, default_delete, shared_ptr, weak_ptr and enable_shared_from_this.)

这篇关于地图不完整的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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