Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overloading members from template class on bool value

I'm trying to avoid concepts on the master branch of my projects, so I need some kind of alternative with type_trait:

I need a class whom some few functions will change depending on the bool value.

Someone already suggest me to split my class, but this case won't make sense here. For some context it's a pool where remove function and some others will change if the object type of the pool can be shared or not.

So I tried to use std::enable_if, but there are still some errors (I want the declaration and the implementation to be separate).

#include  <type_traits>

template < typename Object, bool Shared = false >
class Foo {

  template < bool S = Shared, typename std::enable_if<S>::type* = nullptr >
  void   bar();

  template < bool S = Shared, typename std::enable_if<!S>::type* = nullptr >
  void   bar();
};

template < typename Object,
           bool Shared >
template < bool S, typename std::enable_if<S>::type* = nullptr >
void   Foo<Object, Shared>::bar() {
    //do something
}


template < typename Object,
           bool Shared >
template < bool S, typename std::enable_if<!S>::type* = nullptr >
void   Foo<Object, Shared>::bar() {
  //do nothing
}

int main() {

  Foo<int> test;
  return 0;
}

Test3.cpp:16:33: error: default argument for template parameter for class enclosing ‘void Foo<Object, Shared>::bar()’
 void   Foo<Object, Shared>::bar() {
                                 ^
Test3.cpp:24:33: error: default argument for template parameter for class enclosing ‘void Foo<Object, Shared>::bar()’
 void   Foo<Object, Shared>::bar() {

EDIT: Removed copy/paste error

like image 686
Mathieu Van Nevel Avatar asked Dec 12 '25 05:12

Mathieu Van Nevel


1 Answers

1.The qualification of class name (i.e. Foo<Object, Shared>::) should be deleted for member function declaration inside the class definition.

2.Default template arguments are not allowed for out-of-class definition of member templates, just remove them.

Default parameters are not allowed

  • in the out-of-class definition of a member template (they have to be provided in the declaration inside the class body)

Then

template < typename Object, bool Shared = false >
class Foo {

  template < bool S = Shared, typename std::enable_if<S>::type* = nullptr >
  void  bar();

  template < bool S = Shared, typename std::enable_if<!S>::type* = nullptr >
  void  bar();
};

template < typename Object,
           bool Shared >
template < bool S, typename std::enable_if<S>::type*  >
void   Foo<Object, Shared>::bar() {
    //do something
}

template < typename Object,
           bool Shared >
template < bool S, typename std::enable_if<!S>::type* >
void   Foo<Object, Shared>::bar() {
  //do nothing
}

LIVE

like image 170
songyuanyao Avatar answered Dec 13 '25 21:12

songyuanyao



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!