地图不完整的值类型

地图不完整的值类型

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

问题描述

我遇到以下错误:

  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 ++接受它也没有错。



您可以使用指针对象来构建完整的类型。但您也可以使用一些第三方集合库,这些库允许不完整的类型,例如是。






§17.6.4.8其他功能/ 2:

(明确允许不完整类型的唯一组件是 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:

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

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

08-19 19:02