0

I have this concept and template function

template<typename T>
concept Averageable = std::is_default_constructible<T>::value &&
    requires(T a, T b)
    {
        { a += b } -> std::convertible_to<T>;
        {a / size_t(1)} -> std::convertible_to<T>;
    };

template<typename T> 
requires Averageable<T>
T mean(const T* vals, size_t size) 
{
    T sum();
    for (size_t i{}; i < size; size++)
    {
        sum += vals[i];
    }
    return sum / size;
}

And for some reason calling like this

const int a[]{1,2,3};
const int res = mean<int>(a, 3);

results in the following IntelliSense error

no instance of function template matches the argument list            argument types are: (size_t, int)

I'm using Visual Studio 2022 with c++ 20. I have tried searching for a reason why this is happening endlessly (as well as a proper tutorial for concepts) and have come up short.

I'm new to using concepts/requires so if anyone has any insight as to why this doesn't work or how to properly do it I would greatly appreciate it

Devin M
  • 5
  • 1
  • 4

1 Answers1

1

Your concept/require is fine. The problem you got here is actually most vexing parse. Namely this line:

T sum();

Despite it might look like you are constructing an integer, it's actually declaring a function that returns a T.

To fix it, simply change the parentheses to braces:

T sum{};

Sidenote, in the for loop, you probably wanted i++ instead of size++.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39