Boost logo

Boost :

From: Douglas Gregor (doug.gregor_at_[hidden])
Date: 2006-09-28 19:45:14

On Sep 28, 2006, at 6:09 PM, Andy Little wrote:
> The following doesnt work though ( nor with the std:: versions of
> the Concepts)
> Output is :
> test.cpp: In function 'T sum(T*, int)':
> test.cpp:28: error: no match for 'operator=' in 'result =
> Addable<T>::operator+(result, array[i])'
> test.cpp:19: note: candidates are: typename Assignable<T>::result_type
> Assignable<T>::operator=(T&, const T&)

Right. Let's take a closer look...

> auto concept Addable<typename T> {
> typename result_type;
> result_type operator+(T x, T y);
> };

This says that you can add two T's with operator+, and the result is
a value of some type result_type (we don't know what that is).

> auto concept Assignable<typename T> {
> typename result_type;
> result_type operator=(T& x, T y);
> };

This says we can assign from a T to a T.

> result = result + array[i];

So, result + array[i] returns a value of type
Addable<T>::result_type. However, there's no requirement on sum that
says that we can assign from an Addable<T>::result_type to a T... we
only say that we can assign from a T to a T! There are a couple ways
to fix this. You could make the Assignable concept take two
parameters (e.g., assign to a T from a U), or you could add a
Convertible constraint that says that the result_type needs to be
convertible to T, e.g.,

        std::Convertible<Addable<T>::result_type, T>

I added this to the where clause, and things work as expected.


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