![](/img/trans.png)
[英]non-'constexpr' function std::to_string(int) in a consteval lambda
[英]CMake: `call to non-‘constexpr’ function ‘_Tp std::accumulate`
在 CMake 项目中,我得到 output:
[main] Building folder: cmake-test
[build] Starting build
[proc] Executing command: /usr/bin/cmake --build /home/jonathan/Projects/cmake-test/build --config Debug --target all -- -j 14
[build] Scanning dependencies of target hello_cmake
[build] [ 50%] Building CXX object CMakeFiles/hello_cmake.dir/main.cpp.o
[build] /home/jonathan/Projects/cmake-test/main.cpp: In function ‘int main()’:
[build] /home/jonathan/Projects/cmake-test/main.cpp:25:43: error: ‘constexpr size_t pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]’ called in a constant expression
[build] 25 | constexpr size_t x = pushConstantsSize(pushConstants);
[build] | ~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
[build] /home/jonathan/Projects/cmake-test/main.cpp:9:18: note: ‘constexpr size_t pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]’ is not usable as a ‘constexpr’ function because:
[build] 9 | constexpr size_t pushConstantsSize(std::array<std::variant<uint32_t,float>,NumPushConstants> const& pushConstants) {
[build] | ^~~~~~~~~~~~~~~~~
[build] /home/jonathan/Projects/cmake-test/main.cpp:14:47: error: call to non-‘constexpr’ function ‘_Tp std::accumulate(_InputIterator, _InputIterator, _Tp, _BinaryOperation) [with _InputIterator = const std::variant<unsigned int, float>*; _Tp = long unsigned int; _BinaryOperation = pushConstantsSize(const std::array<std::variant<unsigned int, float>, NumPushConstants>&) [with long unsigned int NumPushConstants = 1; size_t = long unsigned int]::<lambda(std::size_t, auto:23)>]’
[build] 14 | return static_cast<size_t>(std::accumulate(pushConstants.cbegin(),pushConstants.cend(),
[build] | ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] 15 | std::size_t{ 0 },
[build] | ~~~~~~~~~~~~~~~~~
[build] 16 | [size_fn](std::size_t acc, auto const var) { return acc + std::visit(size_fn,var); }
[build] | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[build] 17 | ));
[build] | ~
[build] make[2]: *** [CMakeFiles/hello_cmake.dir/build.make:63: CMakeFiles/hello_cmake.dir/main.cpp.o] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/hello_cmake.dir/all] Error 2
[build] make: *** [Makefile:84: all] Error 2
[build] Build finished with exit code 2
IntelliSense 给出的错误:
主.cpp:
#include <numeric>
#include <array> // std::array
#include <numeric> // std::accumulate
#include <variant> // std::variant
#include <stdint.h> // uint32_t
#include <iostream>
template <size_t NumPushConstants>
constexpr size_t pushConstantsSize(std::array<std::variant<uint32_t,float>,NumPushConstants> const& pushConstants) {
auto size_fn = [](auto const& var) -> size_t {
using T = std::decay_t<decltype(var)>;
return sizeof(T);
};
return static_cast<size_t>(std::accumulate(pushConstants.cbegin(),pushConstants.cend(),
std::size_t{ 0 },
[size_fn](std::size_t acc, auto const var) { return acc + std::visit(size_fn,var); }
));
}
int main() {
constexpr size_t num = 1;
static std::array<std::variant<uint32_t,float>,num> const pushConstants = {
uint32_t { 1 }
};
constexpr size_t x = pushConstantsSize(pushConstants);
std::cout << "size:" << x <<std::endl;
}
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.16)
project (hello_cmake)
set(CMAKE_CXX_STANDARD 20)
add_executable(${PROJECT_NAME} main.cpp)
最小可重现示例项目 zip: https://drive.google.com/file/d/1b33hWUkLgQh8knvKV5k3_jDq1uJIUuTv/view?usp=sharing
在这里实现相同的代码https://gcc.godbolt.org/z/esPd87r4o时,它可以工作,这并不意外,因为这可能是 CMake 配置的问题。
对于std::accumulate
的constexpr
实现,我正在使用 CPP 20 和 GCC 10.2.0,正如它所要求的那样:
用于数值算法的
constexpr
在: https://en.cppreference.com/w/cpp/compiler_support
CMakeLists.txt
包括set(CMAKE_CXX_STANDARD 20)
查看安装在我的系统上的编译器:
jonathan@jonathan-MS-7B22:~$ gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
jonathan@jonathan-MS-7B22:~$ gcc-10 --version
gcc-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
jonathan@jonathan-MS-7B22:~$ g++ --version
g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
jonathan@jonathan-MS-7B22:~$ g++-10 --version
g++-10 (Ubuntu 10.2.0-5ubuntu1~20.04) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
jonathan@jonathan-MS-7B22:~$
CMake 安装:
jonathan@jonathan-MS-7B22:~$ cmake --version
cmake version 3.16.3
CMake suite maintained and supported by Kitware (kitware.com/cmake).
jonathan@jonathan-MS-7B22:~$
我正在使用 Visual Studio 代码CMake 工具扩展来方便,设置如下:
目前我最好的猜测是某些方面默认使用gcc
而不是gcc-10
,这会导致这个问题影响整个项目,但我不知道这可能在哪里或如何解决它。
我对在这里做什么感到不知所措,任何帮助将不胜感激,感谢您阅读本文。
我相信我已经找到了错误的来源,在重新配置项目时我得到了 output:
[main] Configuring folder: cmake-test
[driver] Removing /home/jonathan/Projects/cmake-test/build/CMakeCache.txt
[driver] Removing /home/jonathan/Projects/cmake-test/build/CMakeFiles
[proc] Executing command: /usr/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=/bin/gcc-10 -H/home/jonathan/Projects/cmake-test -B/home/jonathan/Projects/cmake-test/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The C compiler identification is GNU 10.2.0
[cmake] -- The CXX compiler identification is GNU 9.3.0
[cmake] -- Check for working C compiler: /bin/gcc-10
[cmake] -- Check for working C compiler: /bin/gcc-10 -- works
[cmake] -- Detecting C compiler ABI info
[cmake] -- Detecting C compiler ABI info - done
[cmake] -- Detecting C compile features
[cmake] -- Detecting C compile features - done
[cmake] -- Check for working CXX compiler: /bin/c++
[cmake] -- Check for working CXX compiler: /bin/c++ -- works
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: /home/jonathan/Projects/cmake-test/build
注意事项The CXX compiler identification is GNU 9.3.0
,我认为这是最终导致错误的原因,我需要The CXX compiler
设置为 10.2.0 。 我不知道如何改变这一点。
作为更新部分注释The CXX compiler identification is GNU 9.3.0
来解决这个问题你可以做set(CMAKE_CXX_COMPILER "/usr/bin/g++-10")
(来自https://stackoverflow.com/a/22619145/4301453 )并重新加载项目。
这似乎有效。
虽然我不确定这是最好的答案,但它现在有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.