繁体   English   中英

我可以将动态数组用作C ++模板typename吗?

[英]Can I use a dynamic array as C++ template typename?

至于下面的代码:

template<typename PatternType>
cl_int enqueueFillBuffer(
    const Buffer& buffer,
    PatternType pattern,
    ::size_t offset,
    ::size_t size,
    const VECTOR_CLASS<Event>* events = NULL,
    Event* event = NULL) const
{
    cl_event tmp;
    cl_int err = detail::errHandler(
        ::clEnqueueFillBuffer(
            object_, 
            buffer(),
            static_cast<void*>(&pattern),
            sizeof(PatternType), 
            offset, 
            size,
            (events != NULL) ? (cl_uint) events->size() : 0,
            (events != NULL && events->size() > 0) ? (cl_event*) &events->front() : NULL,
            (event != NULL) ? &tmp : NULL),
            __ENQUEUE_FILL_BUFFER_ERR);

    if (event != NULL && err == CL_SUCCESS)
        *event = tmp;

    return err;
}

如果数组长度为6是静态指定的,则可以编译代码。

queue.enqueueFillBuffer<float[6]>(buffer, nodes, 2345, 123456);

我的问题是如何使长度6成为变量并传递编译? 由于C99支持动态数组,因此sizeof(float [n])可以正确获取大小(代码sizeof(PatternType) )。 但我无法使下面的代码通过编译:

int n = 6;
queue.enqueueFillBuffer<float[n]>(buffer, nodes, 2345, 123456);

考虑使用std::array 更一般地,假设类似STL的容器将传递给您的方法。 例如,

std::array<float, 6> nodes;
nodes[0] = ...

要么

std::vector<float> nodes;
nodes.resize(6);
nodes[0] = ...

然后行

static_cast<void*>(&pattern),
sizeof(PatternType),

可以替换为

static_cast<void*>(pattern.data()),
sizeof(typename PatternType::value_type) * pattern.size(), 

然后编译器可以推断出类型,因此调用该方法就变得简单了

queue.enqueueFillBuffer(buffer, nodes, 2345, 123456);

不需要显式模板参数。

答案是无法完成它。 至于enqueueFillBuffer实现,请参考: https ://www.khronos.org/bugzilla/show_bug.cgi?id = 1347支持的模式的最大大小为ulong16,128字节。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM