简体   繁体   中英

Specialize member function of class template using C++20 concepts

I want to specialize a member function of a class template as follows:

#include <concepts>

template <typename T>
struct S {
    void f();
};

template <typename T>
void S<T>::f() {
}

// (0) This is fine.
template <>
void S<int>::f() {
}

// (1) This triggers an error.
template <std::integral T>
void S<T>::f() {
}

The specialization (0) is fine, but specializes f() only for the int type. Instead, I would like to specialize it, eg, for any integral type, as in (1). Is this possible using C++20 concepts? Notice that std::integral is just an example and that my specific case makes use of user-defined concepts.

Simply use a trailing requires-clause. The compiler will choose the most constrained function:

#include <concepts>
#include <iostream>

template <typename T>
struct S {
    void f();
    
    void f() requires std::integral<T>;
};

template <typename T>
void S<T>::f() {
  std::cout << "general\n";
}

template <typename T>
void S<T>::f() requires std::integral<T> {
  std::cout << "constrained\n";
};


int main() {
  S<int> x;
  S<double> y;
  
  x.f(); // prints constrained
  y.f(); // prints general

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM