相关疑难解决方法(0)

模板约束C++

在C#中,我们可以定义一个泛型类型,它对可用作泛型参数的类型施加约束.以下示例说明了泛型约束的用法:

interface IFoo
{
}


class Foo<T> where T : IFoo
{
}

class Bar : IFoo
{
}

class Simpson
{
}

class Program
{
    static void Main(string[] args)
    {
        Foo<Bar> a = new Foo<Bar>();
        Foo<Simpson> b = new Foo<Simpson>(); // error CS0309
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法可以在C++中对模板参数施加约束.


C++ 0x本身支持这个,但我说的是当前的标准C++.

c++ templates constraints

62
推荐指数
6
解决办法
4万
查看次数

是否有可能在c ++中开发静态for循环?

是否有可能存在这样的事情?

template<int Channel>
void deduce_mask(Matrix const &src, int mask[])
{
    //I hope i could become a constant and the compiler would unroll the loop at compile time        
    for(int i = Channel; i != -1; --i)
    {            
        //mapper is a helper class which translate two and three dimension into one dimension index
        //constexpr makes it possible to find out the index at compile time
        mask[mapper(0, 1, i)] = src(row - 1, col)[i];
        mask[mapper(1, 1, i)] = src(row, col)[i];
        mask[mapper(2, 1, i)] …
Run Code Online (Sandbox Code Playgroud)

c++ for-loop template-meta-programming

18
推荐指数
3
解决办法
5452
查看次数