推动::可选不是让我重新分配常量的值类型 [英] boost::optional not letting me reassign const value types

查看:256
本文介绍了推动::可选不是让我重新分配常量的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,应该有四个变量的boost ::可选的

It seems to me there should be four variants of boost::optional


  • 可选<富> =>持有可变Foo和可以初始化后重新分配

  • optional<Foo> => holds a mutable Foo and can be reassigned after initialization

可选&LT;富常量&GT;常量 =>持有一个const Foo和初始化后不能重新分配

optional<Foo const> const => holds a const Foo and can't be reassigned after initialization

可选&LT;富&GT;常量 =>(应该?)持有可变富,但初始化后不能重新分配

optional<Foo> const => (should?) hold a mutable Foo but can't be reassigned after initialization

可选&LT;富常量&GT; =>(?应该)举办一个const Foo和可以初始化后重新分配

optional<Foo const> => (should?) hold a const Foo and can be reassigned after initialization

前2情况下工作正常。但可选&LT;富&GT;常量解引用一个const美孚和可选&LT;富常量&GT; 不允许重新分配初始化后(经的this质疑)。

The first 2 cases work as expected. But the optional<Foo> const dereferences to a const Foo, and the optional<Foo const> doesn't allow reassignment after initialization (as touched upon in this question).

const的值类型的重新分配是具体是什么我碰到了,而且错误是:

The reassignment of the const value types is specifically what I ran into, and the error is:

/usr/include/boost/optional/optional.hpp:486:错误:传递常量富'作为'这个'富与放的说法;美孚::运算符=(常量美孚和放大器;)丢弃预选赛[-fpermissive]

/usr/include/boost/optional/optional.hpp:486: error: passing ‘const Foo’ as ‘this’ argument of ‘Foo& Foo::operator=(const Foo&)’ discards qualifiers [-fpermissive]

和它发生在这里:

void assign_value(argument_type val,is_not_reference_tag) { get_impl() = val; }

建成后,实现使用你参数可选同类型的赋值操作符。这显然​​不希望左手操作数是一个const值。但是,为什么你不应该能够重置非const可选到一个新的常量的值,如本例中:

After construction, the implementation uses the assignment operator for the type you parameterized the optional with. It obviously doesn't want a left-hand operand which is a const value. But why shouldn't you be able to reset a non-const optional to a new const value, such as in this case:

optional<Foo const> theFoo (maybeGetFoo());
while (someCondition) {

    // do some work involving calling some methods on theFoo
    // ...but they should only be const ones

    theFoo = maybeGetFoo();
}

若干问题:


  • 我是正确的,希望这是概念上的罚款,并没有能够做到这一点仅仅是在执行侥幸?

  • Am I right that wanting this is conceptually fine, and not being able to do it is just a fluke in the implementation?

如果我不编辑提振人士透露,这将是实现像在循环逻辑以上无需报废的boost ::可选一个完全干净的方式?

If I don't edit the boost sources, what would be a clean way to implement logic like in the loop above without scrapping boost::optional altogether?

如果这有一定道理,我是编辑的boost ::可选的源(我已经不得不这样做,使它的支持活字的,但我怀疑他们会尽快这样做本身)那么什么微创变化可能做的伎俩?

If this does make sense and I were to edit the boost::optional source (which I've already had to do to make it support movable types, though I suspect they'll be doing that themselves soon) then what minimally invasive changes might do the trick?

推荐答案

(1)一个人的什么行为应该意见取决于自选是否一零容器或者任意类型的一个对象薄代理的类型,带有附加功能的。现有code使用后者的主意,并通过这样做,它将删除列表中的四个不同的行为的一半。这降低了复杂性,让你无意中引入低效使用。

(1) One's opinion on what the behavior "should" be depends on whether optionals are "a container for zero or one objects of an arbitrary type" or "a thin proxy for a type, with an added feature". The existing code uses the latter idea, and by doing so, it removes half of the "four different behaviors" in the list. This reduces the complexity, and keeps you from unintentionally introducing inefficient usages.

(2)对于任何键入其值是拷贝的,人们可以很容易可变和不可变自选之间通过一个新的切换。因此,在给定的情况下,你会得到它作为可变短暂,然后将其复制到一个不变的值。

(2) For any Foo type whose values are copyable, one can easily switch between mutable and immutable optionals by making a new one. So in the given case, you'd get it as mutable briefly and then copy it into an immutable value.

optional<Foo> theMutableFoo (maybeGetFoo());
while (someCondition) {
    optional<Foo const> theFoo (theMutableFoo);

    // do some work involving calling some methods on theFoo
    // ...but they should only be const ones
    // ...therefore, just don't use theMutableFoo in here!

    theMutableFoo = maybeGetFoo();
}

鉴于这是一个薄代理为一个类型的模型,这正是同样的事情,你会做,如果该类型没有包装在一个可选的。一个普通的常数值类型需要在这种情况下同样的待遇。

(3)人们必须跟进href=\"http://stackoverflow.com/a/11461199/211160\">信息由@Andrzej 给出的

(3) One would have to follow up on the information given by @Andrzej to find out. But such an implementation change would probably not perform better than creating a new optional every time (as in the loop above). It's probably best to accept the existing design.

来源:<一个href=\"http://stackoverflow.com/questions/11459270/boostoptional-not-letting-me-reassign-const-value-types/11479925#comment15126003_11459270\">@R.MartinhoFernandez, <一href=\"http://stackoverflow.com/questions/11459270/boostoptional-not-letting-me-reassign-const-value-types/11479925#comment15126051_11459270\">@KerrekSB

这篇关于推动::可选不是让我重新分配常量的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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