Home template specialization compiler error
Reply: 0

template specialization compiler error

user7460
1#
user7460 Published in September 21, 2018, 8:36 am

I have a main.cpp and a header file called bar.h This is the main.cpp. My goal is to have the program print out "bar":

#include "bar.h"

struct Raw{
};

template<typename Obj>
struct IsBar{
    static const bool value = false;
};

template<typename Obj, bool>
struct function{
    static void callbar(Obj obj){ 
        obj.bar();
    }; 
};

template<typename Obj>
struct function<Obj, false>{
    static void callbar(Obj obj){ 
        std::cout<< "no bar()" << std::endl;
    }; 
};

int main()
{
    typedef Bar<Raw> Obj;
    Obj obj;

    function<Obj, IsBar<Obj>::value> f;
    f.callbar(obj); 

    return 0;
}

For the bar.h:

template<typename T>
struct Bar{
    void bar()
    {
        std::cout<< "bar" << std::endl;
    };
};

template<>
struct IsBar<Bar>{ // I know this wouldn't work, but how do I do something like Bar<Raw> instead of just Bar?
    static const bool value = true;
};

Compiler gave me this error: error: 'IsBar' is not a class template. Previously I tried to have the content of bar.h inside the main.cpp, and everything works fine, because Raw is known when I declared the IsBar<> specialization using Bar<Raw>.

share|improve this question
  • The template must be defined before the specialization of it. – immibis Feb 12 at 2:45
  • Of course you can do template<> struct IsBar<Bar<Raw>>, or even template<typename T> struct IsBar<Bar<T>>, but the primary template must be visible when you add the specialization. – Bo Persson Feb 12 at 2:45

1 Answer 1

active oldest votes
up vote 2 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.328526 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO