相关疑难解决方法(0)

优雅的模板专业化

有没有一种优雅的方法来基于其模板参数之一专门化模板?

IE浏览器.

template<int N> struct Junk {
    static int foo() {
        // stuff
        return Junk<N - 1>::foo();
    }
};

// compile error: template argument '(size * 5)' involves template parameter(s)
template<int N> struct Junk<N*5> {
    static int foo() {
        // stuff
        return N;
    }
};

template<> struct Junk<0> {
    static int foo() {
        // stuff
        return 0;
    }
};
Run Code Online (Sandbox Code Playgroud)

IE浏览器.我试图专门化一个基于参数可被5整除的模板.我似乎可以这样做的唯一方法如下:

template<int N> struct JunkDivisibleBy5 {
    static int foo() {
        // stuff
        return N;
    }
};

template<int N> struct Junk { …
Run Code Online (Sandbox Code Playgroud)

c++ templates

4
推荐指数
3
解决办法
2964
查看次数

标签 统计

c++ ×1

templates ×1