2

I try to dispatch between two functions using if constexpr. The dispatcher function should accept for example std::size_t and an arbitrary class type.

It works if I just call it with a scalar type, but if I try to pass a class type it triggers a compile error which is not really helpful to me (please see below).

Please have a look at my current approach:

template <auto Other>
constexpr auto mul() const {
  if constexpr (std::is_scalar_v<decltype(Other)>)
    return mul_with_scalar<Other>();
  else
    return mul_with_matrix<Other>();
}

template <size_t Scalar>
constexpr auto mul_with_scalar() const {
  return apply([](size_t v, auto) { return v * Scalar; },
               std::make_index_sequence<Size>{});
}

template <class Other>
constexpr auto mul_with_matrix() const {
  return size_t{0}; // implement me
}
note: candidate: template<auto Other> constexpr auto matrix<Rows, Cols, Vals>::mul() const [with auto Other = Other; long unsigned int Rows = 3; long unsigned int Cols = 3; long unsigned int ...Vals = {}]
   constexpr auto mul() const {
                  ^~~
./constexpresian/matrix.hpp:81:18: note:   template argument deduction/substitution failed:
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Hamdor
  • 381
  • 4
  • 15

2 Answers2

3

I wan't that the function mul can handle non-type and type parameters.

That is not possible in C++. A template parameter can be a type or a value (or a template), but it cannot be both. template<auto Name> makes Name a value template parameter, the type of whose value will be deduced at the time the value is passed.

But since it is a compile-time value, you can wrap its value in a type. For integer types, std::integer_constant will work. For future C++ revisions that allow other kinds of value parameters, you'll have to use a more generic wrapper.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
1

There is one question I have, you can refer to my comment below your post that pertains to the term Size.

I tried your code above as is even with Size the way it is to see the compiler errors and this is coming from Visual Studio 2017 v4.6.0105 c++17 on Win 7 x64 Home Premium under x86 Debug on an Intel Quad Core Extreme.

1>------ Build started: Project: StackOverflow, Configuration: Debug Win32 ------
1>Learner.cpp
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(75): error C3533: a parameter cannot have a type that contains 'auto'
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(75): error C2270: 'mul': modifiers not allowed on nonmember functions
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(83): error C2270: 'mul_with_scalar': modifiers not allowed on nonmember functions
1>c:\users\skilz80\documents\visual studio 2017\projects\stackoverflow\stackoverflow\learner.h(89): error C2270: 'mul_with_matrix': modifiers not allowed on nonmember functions
1>Done building project "StackOverflow.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Maybe this interpretation of these compiler errors might help you.

Here are some links that might help concerning auto and template parameters:

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59