eliemichel / WebGPU-Cpp

A single-file zero-overhead C++ idiomatic wrapper for WebGPU native
https://eliemichel.github.io/WebGPU-Cpp/
MIT License
301 stars 20 forks source link

About types #4

Open sleeplessai opened 1 year ago

sleeplessai commented 1 year ago
void checkFeatures(wgpu::Adapter adapter) {
    std::vector<wgpu::FeatureName> features;
    size_t featureCount = adapter.enumerateFeatures(nullptr);
    features.resize(featureCount);    /// <<<< HERE comes the error from clangd

    adapter.enumerateFeatures(features.data());

    for (auto feat : features) {
        std::clog << " - " << feat << '\n';
    }
}

I got the error hint about the wgpu::FeatureName default consturctor warning from clangd

In template: no matching function for call to 'construct_at'clang(ovl_no_viable_function_in_call)
stl_construct.h(115, 4): Error occurred here
stl_uninitialized.h(643, 8): In instantiation of function template specialization 'std::_Construct<wgpu::FeatureName>' requested here
stl_uninitialized.h(701, 4): In instantiation of function template specialization 'std::__uninitialized_default_n_1<false>::__uninit_default_n<wgpu::FeatureName *, unsigned long>' requested here
stl_uninitialized.h(779, 19): In instantiation of function template specialization 'std::__uninitialized_default_n<wgpu::FeatureName *, unsigned long>' requested here
vector.tcc(650, 8): In instantiation of function template specialization 'std::__uninitialized_default_n_a<wgpu::FeatureName *, unsigned long, wgpu::FeatureName>' requested here
stl_vector.h(1013, 4): In instantiation of member function 'std::vector<wgpu::FeatureName>::_M_default_append' requested here
adapter.cpp(18, 14): In instantiation of member function 'std::vector<wgpu::FeatureName>::resize' requested here
stl_construct.h(94, 5): Candidate template ignored: substitution failure [with _Tp = wgpu::FeatureName, _Args = <>]: no matching constructor for initialization of 'wgpu::FeatureName'

which makes me unable to use pure C++ binding in an elegant manner. So I had to call the mixed like this:

void checkFeatures(wgpu::Adapter adapter) {
    std::vector<WGPUFeatureName> features;      //// <<< Use C-type WGPUFeatureName
    size_t featureCount = adapter.enumerateFeatures(nullptr);
    features.resize(featureCount);

    wgpuAdapterEnumerateFeatures(adapter, features.data());    ///<<< C API

    for (auto feat : features) {
        std::clog << " - " << feat << '\n';
    }
}

I am not sure if I could use the C++ bindings to finish your nice tutorials. Here is another similar problem on type of wgpu::Surface and glfw3webgpu extension. So I had to use type casting although it works:

wgpu::Surface surface = static_cast<wgpu::Surface>(glfwGetWGPUSurface(instance, window)); 

Please give me some advices on using C or C++ ? Thank you.

eliemichel commented 1 year ago

I'll have a look at this, I should find a way to make clang happy :)