Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested template C++

I have a template class of the form:

template<typename ContainerType>
class ConfIntParamStat {
  public:
    typedef typename ContainerType::Type Type;
...
  private:
    void sample(int iteration) {...}
}

I would like to create a specific version of the function sample for the case when ContainerType is a Vector. Where Vector itself is a template class, but I do not know which type of values this Vector holds.

My intuition was to create this in the header file:

template<typename Type>
ConfIntParamStat<Vector<Type> >::sample(int iteration) {
...
}

But it does not compile, and the error from clang is:

error: nested name specifier 'ConfIntParamStat<Vector<Type> >::' for declaration does not refer into a class, class template or class template partial specialization

Is it possible using another syntax ?

like image 421
vkubicki Avatar asked Jul 01 '16 15:07

vkubicki


2 Answers

If you didnt want to specialize the template and were looking for a member only specialization try the following

#include <iostream>
#include <vector>
using namespace std;

template <typename ContainerType>
class Something {
  public:

    void do_something(int);

    template <typename Which>
    struct do_something_implementation {
        void operator()() {
            cout << "general implementation" << endl;
        }
    };
    template <typename Which>
    struct do_something_implementation<vector<Which>> {
        void operator()() {
            cout << "specialized implementation for vectors" << endl;
        }
    };
};

template <typename ContainerType>
void Something<ContainerType>::do_something(int) {
    do_something_implementation<ContainerType>{}();
}

int main() {
    Something<double> something;
    something.do_something(1);

    return 0;
}

If your intent is to specialize a function, I would just overload the function like so

#include <iostream>
#include <vector>
using namespace std;

template <typename ContainerType>
class Something {
  public:

    void do_something(int);

    template <typename Type>
    void do_something(const vector<Type>&);
};

template <typename ContainerType>
void Something<ContainerType>::do_something(int) {
    cout << "Called the general method for do_something" << endl;
}

template <typename ContainerType>
template <typename Type>
void Something<ContainerType>::do_something(const vector<Type>&) {
    cout << "Called the specialised method" << endl;
}

int main() {
    vector<int> vec{1, 2, 3};
    Something<double> something;
    something.do_something(1);
    something.do_something(vec);

    return 0;
}

This is mostly why full/explicit function template specializations are not required. Overloading allows for almost the same effects!

Note This is a great article related to your question! http://www.gotw.ca/publications/mill17.htm

like image 54
Curious Avatar answered Oct 04 '22 00:10

Curious


You could make use of the overloading mechanism and tag dispatch:

#include <vector>

template <class T>
struct Tag { };

template<typename ContainerType>
class ConfIntParamStat {
  public:
    typedef typename ContainerType::value_type Type;
//...
//  private:
    void sample(int iteration) {
       sample_impl(Tag<ContainerType>(), iteration);
    }

    template <class T>
    void sample_impl(Tag<std::vector<T> >, int iteration) {
       //if vector
    }

    template <class T>
    void sample_impl(Tag<T>, int iteration) {
       //if not a vector
    }
};

int main() {
   ConfIntParamStat<std::vector<int> > cips;
   cips.sample(1);
}

As skypjack mentioned this approach has a little draw when using const. If you are not using c++11 (I suspect you dont because you use > > syntax for nested templates) you could workaround this as follows:

#include <iostream>
#include <vector>

template <class T>
struct Tag { };

template <class T>
struct Decay {
   typedef T Type;
};

template <class T>
struct Decay<const T> {
   typedef T Type;
};

template<typename ContainerType>
class ConfIntParamStat {
  public:
    typedef typename ContainerType::value_type Type;
//...
//  private:
    void sample(int iteration) {
       sample_impl(Tag<typename Decay<ContainerType>::Type>(), iteration);
    }

    template <class T>
    void sample_impl(Tag<std::vector<T> >, int iteration) {
       std::cout << "vector specialization" << std::endl;
    }

    template <class T>
    void sample_impl(Tag<T>, int iteration) {
       std::cout << "general" << std::endl;
    }
};

int main() {
   ConfIntParamStat<const std::vector<int> > cips;
   cips.sample(1);
}
like image 45
W.F. Avatar answered Oct 03 '22 23:10

W.F.