3

That's a really basic question I think, but I was'nt able to find an answer, even on StackOverflow. So sorry if you want to hit me when you'll read this.

I just want to do a partial specialization on bool value :

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

  void bar();
};

template < typename Object >
void  Foo<Object, true>::bar() {}

template < typename Object >
void  Foo<Object, false>::bar() {}

int main() {

  Foo<int> test;
  return 0;
}

I think the idea is correct, but I'm missing something with this code (probably really stupid) :

Test3.cpp:8:30: error: invalid use of incomplete type ‘class Foo<Object, true>’
 void  Foo<Object, true>::bar() {
                              ^
Test3.cpp:2:7: note: declaration of ‘class Foo<Object, true>’
 class Foo {
       ^~~
Test3.cpp:13:31: error: invalid use of incomplete type ‘class Foo<Object, false>’
 void  Foo<Object, false>::bar() {
                               ^
Test3.cpp:2:7: note: declaration of ‘class Foo<Object, false>’
 class Foo {
Mathieu Van Nevel
  • 1,428
  • 1
  • 10
  • 26
  • Shouldn't you be trying to specialize a class? Looks like you are trying to define a function which is part of a specialization. – Paul Rooney Dec 01 '16 at 23:21
  • You cannot partial specialize a function/method, but you could fully specialize it: `template <> void Foo::bar() {}` [Demo](http://coliru.stacked-crooked.com/a/18f9ddb32cf72ddd) – Jarod42 Dec 01 '16 at 23:48

2 Answers2

2

Your template defines a class, not a function. That means that you have to specialize that class, not the class method:

template < typename Object >
class Foo<Object, false> {
  void bar();
};

template < typename Object >
class Foo<Object, true> {
  void bar();
};
Sam Varshavchik
  • 114,536
  • 5
  • 94
  • 148
  • Thanks, I need some rest it seems. – Mathieu Van Nevel Dec 01 '16 at 23:22
  • 2
    No problem. One should never code C++ without getting a full night's sleep and a hearty breakfast, first. You should've been taught that in Computer Science 101. – Sam Varshavchik Dec 01 '16 at 23:23
  • @SamVarshavchik Well, I think what is worth mentioning here is that one cannot do **partial** specialization of a class on method declaration while explicit specialization like [this](http://melpon.org/wandbox/permlink/i0dGa1WaBgEq3jcn) would be perfectly valid. – W.F. Dec 02 '16 at 09:36
2

Another way is to decompose foo and treat the implementation of bar in a separate helper class. This reduces the amount of repetition required to implement Foo.

For example:

template<class Object, bool Shared>
struct implement_bar;

template<class Object>
struct implement_bar<Object, true>
{
  void operator()(Object& o) const 
  {
    // do true thing with o
  }
};

template<class Object>
struct implement_bar<Object, false>
{
  void operator()(Object& o) const 
  {
    // do false thing with o
  }
};

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

  void bar()
  {
    return implement_bar<Object, Shared>()(*this);
  }
};

int main() {

  Foo<int> test;
  return 0;
}
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142