Boost logo

Boost :

From: Gennadiy Rozental (gennadiy.rozental_at_[hidden])
Date: 2003-04-02 01:46:38

> > 2. Top level const requirement on bounded types.
> > It's unreasonable. I should be able to define variant with const
> > types. It will be as usable as usual constant types are. The only
> > requirements that we may incur is that if one types is const,
> > rest should be also.
> It's actually not unreasonable: one of the primary goals of variant is
> to match the semantics of its bounded types as closely as possible. The
> prohibition on top-level const types, then, is quite reasonable.
> To see, consider the following:
> const int i = 3;
> i = 4; // error
> variant<const int> v;
> v = 4; // error?
> If top-level const types *were* allowed, then the assignment would
> succeed. Itay and I decided such was highly undesirable. Let me know if
> you disagree with the above reasoning.

If I understood you properly you having problem with the following code
successful compilation:

template<typename T>
struct A
    char buf[4];

    template<typename Arg>
    operator=( Arg const& arg )
        new (buf) T(arg);

int main() {
    A<const int> a;

    a = 4;

This wouldn't happened if you use reinterpret cast based implementation, but
probably do not want to rely on it. What I would do is to explicitly
prohibit some operations using static asserts for top level const type
situation. It wouldn't cost you anything and allow to support this usage


Boost list run by bdawes at, gregod at, cpdaniel at, john at