0

I would like to use a static method in a template parameter in my function in an enable_if expression. However, the static method is also a template, and I get compiler errors.

The error message:

$ g++ test.cpp
test.cpp:20:36: error: invalid declarator before ‘(’ token 
   20 | std::enable_if_t<A::method_a<param>()>

Code sample:

#include <type_traits>

struct B
{
  template<int param>
  static constexpr int method_a()
  {
    return param == 5;
  }

  static constexpr int method_b(int param)
  {
    return param == 5;
  }
};


// this doesn't work
template<int param, typename A>
std::enable_if_t<A::method_a<param>()>
func_a(A& a)
{}

// this works
template<int param, typename A>
std::enable_if_t<A::method_b(param)>
func_b(A& a)
{}

int main()
{
  B b;
  func_b<5>(b); // this doesn't work
  func_a<5>(b); // this works


  return 0;
}

1 Answers1

1

In func_a, the name method_a is a dependent name i.e. the meaning of method_a depends on the template type parameter A. If the name is a member template, you need to specify that with the template keyword:

template<int param, typename A>
std::enable_if_t<A::template method_a<param>()>
                 // ^^^^^^^^
func_a(A& a){}

Here's a demo.

cigien
  • 57,834
  • 11
  • 73
  • 112