2017-11-11 48 views
0

我有这样一段代码:某种生成循环

template<class T, class color_type> 
void assign_channels(T* ptr, const color_type& color) { 
    *(ptr + 0) = get_channel<0>(color); 

    if constexpr(1 < color_type::size) { 
     *(ptr + 1) = get_channel<1>(color); 
    } 
    if constexpr(2 < color_type::size) { 
     *(ptr + 2) = get_channel<2>(color); 
    } 
    if constexpr(3 < color_type::size) { 
     *(ptr + 3) = get_channel<3>(color); 
    } 
    if constexpr(4 < color_type::size) { 
     *(ptr + 4) = get_channel<4>(color); 
    } 
    if constexpr(5 < color_type::size) { 
     *(ptr + 5) = get_channel<5>(color); 
    } 
    if constexpr(6 < color_type::size) { 
     *(ptr + 6) = get_channel<6>(color); 
    } 
    if constexpr(7 < color_type::size) { 
     *(ptr + 7) = get_channel<7>(color); 
    } 
} 

它有两个明显的缺陷:

  1. ,除非我有不超过8个通道将无法正常工作;
  2. 它主要是样板。

有没有一种方式在C++中重写它在某种类型的循环?我认为一个反复出现的模板结构可以完成这项工作,但它不是很可读。我该怎么办?

+1

为什么'get_channel'是一个模板? o.0' – Alexander

+0

@Alexander这是一个长期以来运行时间最小化的真正灵活的'color_t'。 :| –

+0

表达'*(ptr + 5)'的更常见的方式是'ptr [5]'。 – nwp

回答

4

使用折叠表达式和std::index_sequence在C++ 17

template<class T, class color_type, size_t... Is> 
void assign_channels(T* ptr, const color_type& color, std::index_sequence<Is...>) { 
    ((ptr[Is] = get_channel<Is>(color)), ...); 
} 

template<class T, class color_type> 
void assign_channels(T* ptr, const color_type& color) { 
    assign_channels(ptr, color, std::make_index_sequence<color_type::size>{}); 
} 

此前C++ 17,你必须重写倍的表达作为递归。