2

We are trying to refactor our code and one of the improvements we want is the following: many functions have many arguments but many of them share a common subset. So, we would like to create a structure that would group them. The problem is that some functions need some of the parameters to be const and some not. Some of these functions must be able to call a subset of these functions supplying this parameter-grouping structure, but under the following restriction: a called function cannot "degrade" the constness of this structure (see the following example). Implementing all required variations of this struct solves the problem but not elegantly. One solution we're working on is to use templates, e.g:

template<class A, class B, class C>
struct my_container
{
    A a;
    B b;
    C c;
};

void foo1(my_container<int, char, float const> & my_cont)
{
}

void foo2(my_container<int const, char, float const> & my_cont)
{
    // This should NOT be allowed: we do mind something being const to be treated by the
    // called function as non-const.
    foo1(my_cont);
}

void foo3(my_container<int, char, float> & my_cont)
{
    // This should be allowed: we don't mind something being non-const to be treated by the
    // called function as const.
    foo2(my_cont);
}

Our problem is that foo2 calls foo1 without the compiler complaining, and we'd like the exact opposite. Is this even possible to implement with templates? Is there any other technique?

2
  • It seems to me, if the constness varies individually, perhaps just don't try to group those arguments.
    – visitor
    Nov 22, 2011 at 12:21
  • The template seems to indicate that you are trying to lump together three rather unrelated values? - If you have a real class for related functionality, I don't think one commonly tries to make it partly modifiable. E.g you don't go out of your way to allow a function only to modify x-coordinate of a Point instance but not y. If it was important, you would pass those as separate arguments, I guess.
    – UncleBens
    Nov 22, 2011 at 16:59

3 Answers 3

1

Neither should work. Different instantiations of a template are unreleated types, with no implicit conversions between them. So my_container<int, char, float const>, my_container<int const, char, float const> and my_container<int, char, float> are all unrelated types, with no implicit conversions between them.

It should be possible to work out something using inheritance, using metaprogramming tricks to determine what you inherit from, but I'm not sure how, and I suspect that it would be more effort than it's worth.

1
  • You are right, but nevertheless I quickly tested it. As expected, both do not work either with g++ 4.4. or MSVC 2010.
    – zerm
    Nov 22, 2011 at 10:21
1

Without bordering on undefined behavior, this can be achieved with another level of indirection. Add a view class which references the original members. Constness can be added implicitly, but cannot be removed.

template<class A, class B, class C>
struct my_container
{
    A a;
    B b;
    C c;
};

template <class A, class B, class C>
class my_container_view
{
    A* a_;
    B* b_;
    C* c_;

public:
    template <class A_, class B_, class C_>
    my_container_view(my_container<A_, B_, C_>& source):
        a_(&source.a), b_(&source.b), c_(&source.c)
    {}
    template <class A_, class B_, class C_>
    my_container_view(my_container_view<A_, B_, C_>& source):
        a_(&source.a()), b_(&source.b()), c_(&source.c())
    {}
    A& a() const { return *a_; }
    B& b() const { return *b_; }
    C& c() const { return *c_; }
};

void foo1(my_container_view<int, char, float const> my_cont)
{
    my_cont.a() = 10;
    my_cont.b() = 'a';
    my_cont.c() /*= 3.14*/;
}

void foo2(my_container_view<int const, char, float const> my_cont)
{
    my_cont.a() /*= 10*/;
    my_cont.b() = 'a';
    my_cont.c() /*= 3.14*/;

    //foo1(my_cont); //not allowed
}

void foo3(my_container_view<int, char, float> my_cont)
{
    my_cont.a() = 10;
    my_cont.b() = 'a';
    my_cont.c() = 3.14;
t
    foo2(my_cont);
}

int main()
{
    my_container<int, char, float> mc;
    foo1(mc);
    foo2(mc);
    foo3(mc);
}

(I have my doubts, though, how much this is worth. Normally with classes, either you can modify all its members - and you just don't modify the ones you don't want to -, or you can't modify any. If you want this level of control, you'd rather pass each argument separately - the opposite of what you are doing.)

3
  • I would like to learn what the benefit of your solution is, compared to mine, beside yours not requiring boost? Regarding your remarks, I think the OP made his use-case clear in the first two sentences of his question.
    – zerm
    Nov 22, 2011 at 18:44
  • @zerm: I suspect that such reinterpret_casting between technically unrelated types is technically undefined behavior, though I don't see why it wouldn't work in practice. - As to the doubt: "The problem is that some functions need some of the parameters to be const and some not." - I haven't met such a dilemma if the resulting object really forms a whole. Or, if you want more fine-grained constness control - pass each member separately, so each can have its own constness.
    – UncleBens
    Nov 22, 2011 at 20:45
  • Good point with reinterpret_cast, I fixed my solution, as both types need to be same (beside const'ness) and not only convertible -- this way I see no reason why reinterpret_cast could cause any problems? I'll check what the std says.
    – zerm
    Nov 22, 2011 at 21:36
0

My solution with a bit meta-programming. Looks pretty ugly and not throughoutly tested, but anyways: This way, everything should work in general "out of the box"

#include <iostream>
#include <boost/type_traits.hpp>

template<class A, class B, class C>
struct my_container
{
    A a;
    B b;
    C c;

    template<typename An, typename Bn, typename Cn>
    operator my_container<An,Bn,Cn>& ()
    {
        /* First, check whether compatible at all */
        BOOST_STATIC_ASSERT((boost::is_same<typename boost::remove_cv<A>::type, typename boost::remove_cv<An>::type>::value));
        BOOST_STATIC_ASSERT((boost::is_same<typename boost::remove_cv<B>::type, typename boost::remove_cv<Bn>::type>::value));
        BOOST_STATIC_ASSERT((boost::is_same<typename boost::remove_cv<C>::type, typename boost::remove_cv<Cn>::type>::value));
        /* Enforce const'ness */
        BOOST_STATIC_ASSERT( !boost::is_const<A>::value || boost::is_const<An>::value );
        BOOST_STATIC_ASSERT( !boost::is_const<B>::value || boost::is_const<Bn>::value );
        BOOST_STATIC_ASSERT( !boost::is_const<C>::value || boost::is_const<Cn>::value );

        return *reinterpret_cast< my_container<An,Bn,Cn>* >(this);
    }
};

void foo1(my_container<int, char, float const> & my_cont)
{
}

void foo2(my_container<int const, char, float const> & my_cont)
{
    // This should NOT be allowed: we do mind something being const to be treated by the
    // called function as non-const.
    //foo1(my_cont); /// BOOST_STATIC_ASSERT fails! Hurray!
}

void foo3(my_container<int, char, float> & my_cont)
{
    // This should be allowed: we don't mind something being non-const to be treated by the
    // called function as const.
    foo2(my_cont); /// No complaints! Hurray!
}

int main(int argc, char* argv[])
{
    my_container<int,char,float> foobar;

    foo3(foobar);

    return 0;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.