c++ - C++20 compile time retrieve size of a vector - Stack Overflow
I have a question regarding to the compile time retrieving size of a vector
in C++20. Based on C++20 it is possible to create in compile time a vector
or a string
in the scope of a function.
There is the following piece of code
#include <iostream>
#include <vector>
#include <array>
consteval auto copyVecToArray(){
std::vector<int> vec{1,2,3,4,5,6,7};
std::array<int,vec.size()> arr;
std::copy(std::begin(vec),std::end(vec),std::begin(arr));
return arr;
}
int main() {
constexpr auto value = copyVecToArray();
for(auto el : value){
std::cout << el << std::endl;
}
}
Demo which is failed with following compilation error
> <source>:7:28: in 'constexpr' expansion of
> 'vec.std::vector<int>::size()'
> /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/stl_vector.h:993:32:
> error: the value of 'vec' is not usable in a constant expression 993
> | { return size_type(this->_M_impl._M_finish -
> this->_M_impl._M_start); }
> | ~~~~~~^~~~~~~ <source>:6:22: note: 'vec' was not declared 'constexpr'
> 6 | std::vector<int> vec{1,2,3,4,5,6,7};
> | ^~~ <source>:7:28: note: in template argument for type 'long unsigned int'
> 7 | std::array<int,vec.size()> arr;
> | ~~~~~~~~^~ <source>:8:55: error: no matching function for call to 'begin(int&)'
> 8 | std::copy(std::begin(vec),std::end(vec),std::begin(arr));
> |
But if I change a bit the code above and return the size of the vector
from a consteval
function this successfully compiles.
#include <iostream>
#include <vector>
#include <array>
consteval auto getVec(){
std::vector<int> vec{1,2,3,4,5,6,7};
return vec;
}
consteval auto getSize(){
return getVec().size();
}
consteval auto copyVecToArray(){
std::array<int,getVec().size()> arr;
auto vec = getVec();
std::copy(std::begin(vec),std::end(vec),std::begin(arr));
return arr;
}
int main() {
constexpr auto value = copyVecToArray();
for(auto el : value){
std::cout << el << std::endl;
}
}
My question is why using consteval
function to retrieve size is ok and not using directly the std::vector::size()
method.
Demo 2
I have a question regarding to the compile time retrieving size of a vector
in C++20. Based on C++20 it is possible to create in compile time a vector
or a string
in the scope of a function.
There is the following piece of code
#include <iostream>
#include <vector>
#include <array>
consteval auto copyVecToArray(){
std::vector<int> vec{1,2,3,4,5,6,7};
std::array<int,vec.size()> arr;
std::copy(std::begin(vec),std::end(vec),std::begin(arr));
return arr;
}
int main() {
constexpr auto value = copyVecToArray();
for(auto el : value){
std::cout << el << std::endl;
}
}
Demo which is failed with following compilation error
> <source>:7:28: in 'constexpr' expansion of
> 'vec.std::vector<int>::size()'
> /opt/compiler-explorer/gcc-14.2.0/include/c++/14.2.0/bits/stl_vector.h:993:32:
> error: the value of 'vec' is not usable in a constant expression 993
> | { return size_type(this->_M_impl._M_finish -
> this->_M_impl._M_start); }
> | ~~~~~~^~~~~~~ <source>:6:22: note: 'vec' was not declared 'constexpr'
> 6 | std::vector<int> vec{1,2,3,4,5,6,7};
> | ^~~ <source>:7:28: note: in template argument for type 'long unsigned int'
> 7 | std::array<int,vec.size()> arr;
> | ~~~~~~~~^~ <source>:8:55: error: no matching function for call to 'begin(int&)'
> 8 | std::copy(std::begin(vec),std::end(vec),std::begin(arr));
> |
But if I change a bit the code above and return the size of the vector
from a consteval
function this successfully compiles.
#include <iostream>
#include <vector>
#include <array>
consteval auto getVec(){
std::vector<int> vec{1,2,3,4,5,6,7};
return vec;
}
consteval auto getSize(){
return getVec().size();
}
consteval auto copyVecToArray(){
std::array<int,getVec().size()> arr;
auto vec = getVec();
std::copy(std::begin(vec),std::end(vec),std::begin(arr));
return arr;
}
int main() {
constexpr auto value = copyVecToArray();
for(auto el : value){
std::cout << el << std::endl;
}
}
My question is why using consteval
function to retrieve size is ok and not using directly the std::vector::size()
method.
Demo 2
Share Improve this question edited yesterday Some programmer dude 409k35 gold badges413 silver badges642 bronze badges asked yesterday getsoublgetsoubl 94512 silver badges29 bronze badges 1- fyi, the way people do this is using a lambda to get around exactly that. example with lambda, i am guessing that the rvalues are constexpr but once it becomes an lvalue it is no-longer constexpr, also see Youtube Understanding The constexpr 2-Step - Jason Turner - C++ on Sea 2024 for a neater way to do this without invoking the lambda twice, useful for complex lambdas. – Ahmed AEK Commented yesterday
1 Answer
Reset to default 2Local variables even in consteval
functions are not constant expressions because they are able to depend on the values of the function’s parameters. It breaks the type system to let them be such. Nor can you work around this (even when the parameter values aren’t needed) by declaring the std::vector
constexpr
, since only empty vectors can be complete constant expressions (for deeply technical reasons). Thus the compute-twice trick.
- 网络购票冲击下的铁路代售点被迫转型
- 云计算是新的商业基础设施
- java - Difficulty Embedding ICC Profile into PDF Using PDFBox, iText, and Ghostscrip - Stack Overflow
- apache beam - Unable to Write processed Data to Parquet - Stack Overflow
- html - Audio tag working in Chrome but not Safari? - Stack Overflow
- reactjs - When i run the build command on my project, the dist files aren't being created in the correct format - Stack
- javascript - Return a promise that resolves when a event is fired - Stack Overflow
- swift - How to Add .mlmodel File to Xcode App Playgrounds (.swiftpm) Project? - Stack Overflow
- testrigor - Exception upon running tests to run database query - Stack Overflow
- android - How to setImageCaptureResolutionSelector() in CameraController? - Stack Overflow
- c - MPI Program Hangs after MPI_Finalize - Stack Overflow
- visual studio code - Cant use pip install on pytorch Python 3.13.01, MacOS Sonoma 14.6.1 - Stack Overflow
- c# - Why is OntriggerEnter2D not working on my Flappy Bird game?Or if not what is the problem and can you solve it? - Stack Over
- node.js - How to handle time taking processes in slack app view - Stack Overflow
- ios - Persist overlay view in the detail side of NavigationSplitView - Stack Overflow
- java - Android physical keyboard support for key press and hold - Stack Overflow
- command prompt - byobu - how to disable byobu_prompt_runtime - Stack Overflow