2

I'm trying to add a partial specialization to a quite complex class. I tried to simplify it already.

Compiler gives the error:

main.cpp:43:39: error: invalid use of incomplete type 'struct Baz' Baz::doSomething(const Foo::type& a)

main.cpp:22:8: note: declaration of 'struct Baz' struct Baz {

I'm constrained in C++ 03, how can I make this right?

#include <iostream>

using namespace std;

struct Foo {
    typedef int type;
};

struct Bar {
    typedef int type;
};

template <typename A, typename B>
struct Baz {

    template <typename V>
    struct ReturnType {
        typedef typename V::type type;
    };

    typedef typename ReturnType<B>::type RtType;

    RtType doSomething(const typename A::type& a);
};

template <typename A, typename B>
typename Baz<A, B>::RtType
Baz<A, B>::doSomething(const typename A::type &a)
{
    cout << "In templated implementation" << endl;
}

//// The above is working fine
//// The below is what I'm trying to specializae, and it doens't work.

template <typename B>
typename Baz<Foo, B>::RtType
Baz<Foo, B>::doSomething(const Foo::type& a)
{
    cout << "In partial specialization" << endl;
}

int main()
{
    Baz<Foo, Bar> baz;
    baz.doSomething(3);

    return 0;
}
Yong Li
  • 607
  • 3
  • 15
  • I created this code in an online C++ editor if someone would like to try it: https://onlinegdb.com/HyGgszpQG – Yong Li Jan 05 '18 at 15:34
  • Please reduce the code involved and produce a [mcve] – Passer By Jan 05 '18 at 15:34
  • The original code file is much more complex and involved, I've tried to produce a minimal example already... I'm not very experienced in template programming and not sure if I can further reduce the scope... – Yong Li Jan 05 '18 at 15:36
  • 2
    For future reference, the linked question is a much more minimal example – Passer By Jan 05 '18 at 15:39

1 Answers1

3

You are trying to partially specialize a method within a template class. This is not possible. You have to partially specialize the whole class.

SergeyA
  • 61,605
  • 5
  • 78
  • 137