std :: unordered_map :: emplace与私有/删除拷贝构造函数的问题 [英] std::unordered_map::emplace issue with private/deleted copy constructor

查看:189
本文介绍了std :: unordered_map :: emplace与私有/删除拷贝构造函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码使用gcc 4.7.2编译(mingw)

  #include< unordered_map> 
#include< tuple>

struct test
{
test()= default;
private:
test(test const&)= delete;
};

int main()
{
std :: unordered_map< char,test>地图;

map.emplace(
std :: piecewise_construct,$ b $ std :: forward_as_tuple('a'),
std :: forward_as_tuple()
) ;

$ / code>

如果我更改 test 中的拷贝构造函数, code>从 test(test const&)= delete; to test(test const&)= default; >但是,模板错误呕吐似乎抱怨 const test& 不能转换为 test (text here )。不应该工作?或者如果不是,他们不应该都会给出错误吗?

会看到这块胡萝卜:

  test.exe.cpp:8:3:error:'constexpr test :: test(const test&)'是私有的

这是问题的线索。



GCC 4.7.2不会将访问检查作为模板参数推导的一部分(如C ++ 03所要求的那样)。 is_convertible trait是使用SFINAE实现的,它依赖于模板参数推导,如果重载解析选择一个私有构造函数参数,则扣除成功,但是由于所选构造函数是私有的,因此访问检查失败。这是GCC 4.7的一个问题,因为它没有被修改为遵循14.8.2 [temp.deduct]中的新C ++ 11规则:


-8-如果替换导致无效类型或表达式,则输入扣减失败。无效的类型或表达式是使用替代参数编写的格式不正确的类型或表达式。 [注意:访问检查是作为替换过程的一部分完成的。

这是对以前的扣除规则的巨大变化,以前的段落说


-8-如果替换导致无效类型或表达式,则输入减法失败。无效的类型或表达式是使用替代参数编写的格式不正确的类型或表达式。访问检查不是作为替代过程的一部分来完成的。因此,当扣减成功时,在函数实例化时仍然可能导致访问错误。


变更在C ++ 0x通过 DR 1170 进行处理,并使SFINAE完全令人敬畏C ++ 11:)

GCC 4.8实现了新规则,所以 is_convertible 和类似的特征给出了正确的答案为无法访问的构造函数。


The following code compiles fine with gcc 4.7.2 (mingw)

#include <unordered_map>
#include <tuple>

struct test
{
        test() =default;
    private:
        test(test const&) =delete;
};

int main()
{
    std::unordered_map<char, test> map;

    map.emplace(
        std::piecewise_construct,
        std::forward_as_tuple('a'),
        std::forward_as_tuple()
    );
}

If I change the copy constructor in test from test(test const&) =delete; to test(test const&) =default; however, the template error vomit seems to complain about const test& not being convertible to test (text here). Shouldn't either work? Or if not, shouldn't they both give an error?

解决方案

If you look at the template error vomit more carefully you'll see this chunk of carrot in it:

test.exe.cpp:8:3: error: 'constexpr test::test(const test&)' is private

This is the clue to the problem.

GCC 4.7.2 doesn't do access checking as part of template argument deduction (as was required by C++03.) The is_convertible trait is implemented using SFINAE, which relies on template argument deduction, and if overload resolution chooses a private constructor argument deduction succeeds, but then access checking fails because the chosen constructor is private. This is a problem with GCC 4.7 because it hadn't been changed to follow the new C++11 rule in 14.8.2 [temp.deduct] which says:

-8- If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. [ Note: Access checking is done as part of the substitution process. —end note ]

This is a huge change to the previous deduction rules, previously that paragraph said

-8- If a substitution results in an invalid type or expression, type deduction fails. An invalid type or expression is one that would be ill-formed if written using the substituted arguments. Access checking is not done as part of the substitution process. Consequently, when deduction succeeds, an access error could still result when the function is instantiated.

The change was made quite late in the C++0x process by DR 1170, and makes SFINAE totally awesome in C++11 :)

GCC 4.8 implements the new rules, so is_convertible and similar traits give the right answer for inaccessible constructors.

这篇关于std :: unordered_map :: emplace与私有/删除拷贝构造函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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