Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板专门化编译器错误_C++_Templates_Metaprogramming_Template Specialization - Fatal编程技术网

C++ 模板专门化编译器错误

C++ 模板专门化编译器错误,c++,templates,metaprogramming,template-specialization,C++,Templates,Metaprogramming,Template Specialization,我有一个main.cpp和一个名为bar.h的头文件 这是main.cpp。我的目标是让程序打印出“bar”: #包括“bar.h” 结构原始{ }; 模板 结构IsBar{ 静态常量布尔值=假; }; 模板 结构函数{ 静态无效调用条(Obj Obj){ obj.bar(); }; }; 模板 结构函数{ 静态无效调用条(Obj Obj){ std::cout你想要的,例如 模板 结构IsBar{ 静态常量布尔值=真; }; 这意味着对于Bar的所有实例化,IsBar::value是tru

我有一个main.cpp和一个名为bar.h的头文件 这是main.cpp。我的目标是让程序打印出“bar”:

#包括“bar.h”
结构原始{
};
模板
结构IsBar{
静态常量布尔值=假;
};
模板
结构函数{
静态无效调用条(Obj Obj){
obj.bar();
}; 
};
模板
结构函数{
静态无效调用条(Obj Obj){
std::cout你想要的,例如

模板
结构IsBar{
静态常量布尔值=真;
};
这意味着对于
Bar
的所有实例化,
IsBar::value
true

注意,主模板应该在专门化之前声明;我认为应该将
IsBar
的主模板定义从
main.cpp
移动到
bar.h


模板必须在专门化之前定义。当然,您可以执行
template struct IsBar
,甚至
template struct IsBar
,但在添加专门化时,主模板必须可见。我仍然收到相同的错误,错误是:“IsBar”不是类template@user1701840修改后的答复.
#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;
}
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;
};
template<typename T>
struct IsBar<Bar<T>> {
    static const bool value = true;
};