创建接收模板类作为参数的模板方法(对于单例类)的最佳方法

发布于 12-21 17:33 字数 829 浏览 1 评论 0原文

我有一个单例类,这是我的主要引擎。

因为我使用 D3D11,并且它使用相同的 ID3D11Device 方法来创建所有缓冲区(无论类型),所以我尝试创建一个模板方法来创建缓冲区。

我用作缓冲区源的也是 std::array

所以到目前为止我尝试的是:

template <size_t Size, typename T>
void CreateBuffer(BufferType bufferType, const std::array <T, Size>& source, ID3D11Buffer** out) {
    (...)
    bd.BindFlags = bufferType;
    bd.ByteWidth = sizeof(T) * source.size();
    (...)
}

然后我像这样使用它:

ID3D11Buffer* buffer = nullptr;
array <SimpleVertex, 10> data; //this is the source data, 10 simple vertices
D3DEngine::GetInstance().CreateBuffer <10> (D3DEngine::Vertex, data, &buffer);

这有效,但看起来很丑。 “10”作为参数模板迫使我“硬编码”大小(我什至不能使用 因为它需要一个常量作为模板参数)。

有更好的方法来实现我想要的吗?或者我应该使用不同的方法? 谢谢。

i have a singleton class thats my main engine.

since im using D3D11, and it uses the same ID3D11Device method to create all buffers (no matter the type), im trying to create a template method to create the buffers.

also what i use as source for the buffer is a std::array

so what im trying so far is:

template <size_t Size, typename T>
void CreateBuffer(BufferType bufferType, const std::array <T, Size>& source, ID3D11Buffer** out) {
    (...)
    bd.BindFlags = bufferType;
    bd.ByteWidth = sizeof(T) * source.size();
    (...)
}

and then i use it like:

ID3D11Buffer* buffer = nullptr;
array <SimpleVertex, 10> data; //this is the source data, 10 simple vertices
D3DEngine::GetInstance().CreateBuffer <10> (D3DEngine::Vertex, data, &buffer);

this works, but looks so ugly. that "10" as argument template forces me "hardcode" the size (i cant even use <data.size()> because it requires a constant as template argument).

is there a better way to achieve what i want? or i should use a different approach?
thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟2024-12-28 17:33:38

编译器应该能够推断大小和类型参数。

为什么你的引擎是单例的?这没什么意义。您可以只返回指针,而不是获取指向它的指针。还有一个原始指针而不是资源管理指针?很高兴我不维护你的代码。

The compiler should be able to infer both size and type arguments.

Why is your engine a singleton? There's little point in that. And you could just return the pointer, instead of taking a pointer to it. And a raw instead of resource-managing pointer? Glad I don't maintain your code.

淡紫姑娘!2024-12-28 17:33:38

我做了一个快速 测试用例,它似乎无需手动指定数组的大小即可工作。

这是代码:

#include <iostream>
#include <string>
#include <array>

template<size_t S, typename T> size_t CreateBuffer(std::string const & s, std::array<T, S> const & source)
{
    // do something
    return S;
}

int main(int argc, char ** argv)
{
    std::array<std::string, 10> arr;
    size_t ret = CreateBuffer("my string", arr);
    std::cout<<"Size: "<<ret;
    return 0;
}

I made a quick test-case and it seems to work without manually specifying the size of the array.

This is the code:

#include <iostream>
#include <string>
#include <array>

template<size_t S, typename T> size_t CreateBuffer(std::string const & s, std::array<T, S> const & source)
{
    // do something
    return S;
}

int main(int argc, char ** argv)
{
    std::array<std::string, 10> arr;
    size_t ret = CreateBuffer("my string", arr);
    std::cout<<"Size: "<<ret;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文