Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将'std::accumulate'与constexpr一起使用 起初的_C++_Templates - Fatal编程技术网

C++ 将'std::accumulate'与constexpr一起使用 起初的

C++ 将'std::accumulate'与constexpr一起使用 起初的,c++,templates,C++,Templates,给定一个std::array我得到总字节大小(例如{float,float,uint32\u t}=4+4+4=12),然后我想以void*的形式得到数组(传递给Vulkan) 我认为我不需要为此进行堆分配,尽管我一直在研究如何将计算出的大小作为模板参数传递 答复: 错误: > clang++-7 -pthread -std=c++17 -o main main.cpp main.cpp:42:30: error: constexpr variable 'size' must be ini

给定一个
std::array
我得到总字节大小(例如
{float,float,uint32\u t}=4+4+4=12
),然后我想以
void*
的形式得到数组(传递给Vulkan)

我认为我不需要为此进行堆分配,尽管我一直在研究如何将计算出的大小作为模板参数传递

答复:

错误:

> clang++-7 -pthread -std=c++17 -o main main.cpp
main.cpp:42:30: error: constexpr variable 'size' must be initialized by a
      constant expression
    constexpr uint32_t const size = arrSize<num>(arr);
                             ^      ~~~~~~~~~~~~~~~~~
main.cpp:17:34: note: non-constexpr function 'accumulate<const
      std::variant<unsigned int, float> *, unsigned long, (lambda at
      main.cpp:19:9)>' cannot be used in a constant expression
    return static_cast<uint32_t>(std::accumulate(arr.cbegin(),arr.cend(),
                                 ^
main.cpp:42:37: note: in call to 'arrSize(arr)'
    constexpr uint32_t const size = arrSize<num>(arr);
                                    ^
/usr/bin/../lib/gcc/x86_64-linux-gnu/8/../../../../include/c++/8/bits/stl_numeric.h:146:5: note: 
      declared here
    accumulate(_InputIterator __first, _InputIterator __last, _Tp __init,
    ^
main.cpp:44:21: error: no matching function for call to 'toVoidPtr'
    void* voidArr = toVoidPtr<num,size>(arr);
                    ^~~~~~~~~~~~~~~~~~~
main.cpp:25:7: note: candidate template ignored: invalid explicitly-specified
      argument for template parameter 'Size'
void* toVoidPtr(std::array<std::variant<uint32_t,float>,Num> const& arr) {
      ^
2 errors generated.
compiler exit status 1
我通过以下方式获得尺寸:

模板
constexpr uint32_t arrSize(标准::数组const&arr){
自动变量大小=[](自动常量和变量)->大小{
使用T=std::decation\u T;
返回大小f(T);
};
返回static_cast(标准::累加(arr.cbegin(),arr.cend(),
std::size_t{0},
[var_size](std::size_t acc,auto const var){return acc+std::visit(var_size,var);}
));
}
我通过以下方式转换为
void*

模板
void*toVoidPtr(标准::数组常量和arr){
size\u t字节计数器=size\u t{0};
std::数组字节;
std::for_each(arr.cbegin()、arr.cend()、[&](自动常量和变量){
标准::访问([&](自动常量和变量){
使用T=std::decation\u T;
std::memcpy(bytes.data()+字节计数器、static_cast(&var)、sizeof(T));
字节计数器+=sizeof(T);
},var);
});
返回静态_cast(bytes.data);
}
我想遵守如下命令:

intmain(){
std::cout std::array::cbegin(),(&pushConstants)->std::array::cend(),0,Utility::pushConstantsSize::{Utility::pushConstantsSize::()}
[build]c:\users\jonat\projects\gpu\blas\c++\example.hpp:367:38:错误:“pushConstant”不是常量表达式
[build]367 | constexpr uint32_t const sizeTester=实用工具::pushConstantsSize(pushConstant);
[建设]|^~~~~~~~~~
[build]c:\users\jonat\projects\gpu blas\c++\example.hpp:在“ComputeApp::ComputeApp(const char*,uint32_t,const uint32_t*,float**,std::array,std::array,std::array,std::array)[带无符号int NumPushConstants=3;uint32_t=unsigned int]:
[build]C:\Users\jonat\Projects\gpu\blas\C++\test\ExampleTests.cpp:849:5:此处为必填项
[build]c:\users\jonat\projects\gpu\blas\c++\example.hpp:367:95:Utility::pushConstantsSize(pushConstant)的“constepr”扩展中
[build]c:\users\jonat\projects\gpu blas\c++\example.hpp:102:53:在'constexpr'中扩展'std::accumulate(&pushConstants)->std::array::cbegin(),(&pushConstants)->std::array::cend(),0,Utility::pushConstantsSize:{Utility::pushConstantsSize::()}
[build]c:\users\jonat\projects\gpu\blas\c++\example.hpp:367:38:错误:“pushConstant”不是常量表达式
[build]mingw32 make[2]:***[test\CMakeFiles\ExampleTests.dir\build.make:82:test/CMakeFiles/ExampleTests.dir/ExampleTests.cpp.obj]错误1
[build]mingw32 make[1]:***[CMakeFiles\Makefile2:315:test/CMakeFiles/ExampleTests.dir/all]错误2
[build]mingw32 make:**[makefile:159:all]错误2
[生成]生成已完成,退出代码为2
我已经在这里多玩了一点,但是我没有发现问题所在,再次感谢大家的帮助

由于我无法给出一个Repl来最低限度地重现这个错误,这里有一个指向当前分支产生这些错误的CMake项目的链接:

acculate
只是
constexpr
自C++20以来,而不是在C++17中。因此,您需要切换到C++20


如果您想知道哪个编译器已经支持
constexpr-acculate
,您可以查看并搜索“constexpr-for-numeric algorithms”/P1645R1。例如,对于clang,它需要libc++12。

清楚地注意到,
acculate
只是
constexpr
,因为C++20,而不是在C++17中。@WernerHenze使用C++20
clang++-7-pthread-std=C+2a-o main.cpp
编译会导致相同的错误。来自“数值算法的constexpr P1645R1”:需要clang libc++12。在更新编译器后,我正在努力解决本地cmake项目中的一系列新问题,如果您想回答这个问题,一旦我能够让它工作,我会将其标记为这样(可能需要一段时间才能让它工作,尽管
忍者:错误
会很痛苦)我更新了使用
c++20
和您建议的兼容编译器(对于我的本地项目,这是
GCC10.2
),我发现了不同的错误,这至少是到目前为止的进步。我在问题中增加了一个部分,涵盖了这些新错误。@JonathanWoollett light在我看来,您原来的问题得到了回答。您的新问题是不同的问题,应该作为不同的问题发布。关于您的新错误消息:How should编译器在编译时计算
constepr uint32_t conste sizeter=Utility::pushConstantsSize(pushConstant);
pushConstant
仅为
const
而非
constepr
时,因此在编译时未知?
[build] In file included from C:\Users\jonat\Projects\gpu_blas\c++\test\ExampleTests.cpp:2:
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp: In instantiation of 'ComputeApp<NumPushConstants>::ComputeApp(const char*, uint32_t, const uint32_t*, float**, std::array<std::variant<unsigned int, float>, NumPushConstants>, std::array<unsigned int, 3>, std::array<unsigned int, 3>) [with unsigned int NumPushConstants = 1; uint32_t = unsigned int]':
[build] C:\Users\jonat\Projects\gpu_blas\c++\test\ExampleTests.cpp:41:5:   required from here
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:367:95:   in 'constexpr' expansion of 'Utility::pushConstantsSize<1>(pushConstant)'
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:102:53:   in 'constexpr' expansion of 'std::accumulate<const std::variant<unsigned int, float>*, long long unsigned int, Utility::pushConstantsSize<1>::<lambda(std::size_t, auto:43)> >((& pushConstants)->std::array<std::variant<unsigned int, float>, 1>::cbegin(), (& pushConstants)->std::array<std::variant<unsigned int, float>, 1>::cend(), 0, <lambda closure object>Utility::pushConstantsSize<1>::<lambda(std::size_t, auto:43)>{<lambda closure object>Utility::pushConstantsSize<1>::<lambda(const auto:42&)>()})'
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:367:38: error: 'pushConstant' is not a constant expression
[build]   367 |             constexpr uint32_t const sizeTester = Utility::pushConstantsSize<NumPushConstants>(pushConstant);
[build]       |                                      ^~~~~~~~~~
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp: In instantiation of 'ComputeApp<NumPushConstants>::ComputeApp(const char*, uint32_t, const uint32_t*, float**, std::array<std::variant<unsigned int, float>, NumPushConstants>, std::array<unsigned int, 3>, std::array<unsigned int, 3>) [with unsigned int NumPushConstants = 3; uint32_t = unsigned int]':
[build] C:\Users\jonat\Projects\gpu_blas\c++\test\ExampleTests.cpp:849:5:   required from here
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:367:95:   in 'constexpr' expansion of 'Utility::pushConstantsSize<3>(pushConstant)'
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:102:53:   in 'constexpr' expansion of 'std::accumulate<const std::variant<unsigned int, float>*, long long unsigned int, Utility::pushConstantsSize<3>::<lambda(std::size_t, auto:43)> >((& pushConstants)->std::array<std::variant<unsigned int, float>, 3>::cbegin(), (& pushConstants)->std::array<std::variant<unsigned int, float>, 3>::cend(), 0, <lambda closure object>Utility::pushConstantsSize<3>::<lambda(std::size_t, auto:43)>{<lambda closure object>Utility::pushConstantsSize<3>::<lambda(const auto:42&)>()})'
[build] c:\users\jonat\projects\gpu_blas\c++\example.hpp:367:38: error: 'pushConstant' is not a constant expression
[build] mingw32-make[2]: *** [test\CMakeFiles\ExampleTests.dir\build.make:82: test/CMakeFiles/ExampleTests.dir/ExampleTests.cpp.obj] Error 1
[build] mingw32-make[1]: *** [CMakeFiles\Makefile2:315: test/CMakeFiles/ExampleTests.dir/all] Error 2
[build] mingw32-make: *** [makefile:159: all] Error 2
[build] Build finished with exit code 2