Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-09-28 18:09:32


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&)

----------------------------------
#include <concepts>
#include <iostream>

#if (0)
//Works
auto concept Addable<typename T> {
  T operator+(T x, T y);
};
#else
// Fails
auto concept Addable<typename T> {
  typename result_type;
  result_type operator+(T x, T y);
};
#endif

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

template<std::CopyConstructible T>
where Addable<T> && Assignable<T>
T sum(T array[], int n)
{
    T result = T(0);
    for (int i = 0; i < n; ++i)
        result = result + array[i];
    return result;
}

int main()
{
  int arr[3] = {1,2,3};
  std::cout << sum(arr, 3);

  return 0;
}

regards
Andy Little


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk