-
December 3rd, 2021, 06:45 AM
#1
Sudden problem with std::vector
Code:
uint32_t some_func() { return 32; }
int buf_1[32]; // <--- this always compiled with MSVC
int buf_2[some_func()]; // <--- whereas this fails because the buffer size needs to be known at compile time
In the past I always got around this by using std::vector - i.e.
Code:
uint32_t some_func() { return 32; }
const int x = some_func(); // <--- I'm sure these lines
std::vector<int> buf_3[x]; // <--- previously compiled
But all of a sudden this morning, the std::vector line is giving me C2131 because the value of x isn't known at compile time. I'm probably missing something obvious but I'm sure this used to compile okay... can anyone see what I'm doing wrong?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 3rd, 2021, 08:56 AM
#2
Re: Sudden problem with std::vector
Oops - I was using square brackets instead of round
This works...
Code:
std::vector<int> buf_3(x);
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 3rd, 2021, 11:58 AM
#3
Re: Sudden problem with std::vector
Originally Posted by John E
Code:
int buf_2[some_func()]; // <--- whereas this fails because the buffer size needs to be known at compile time
It will work (in this case) if the function is declared constexpr.
-
December 4th, 2021, 07:36 AM
#4
Re: Sudden problem with std::vector
Thanks for the tip wolle. I'd never even heard of constexpr but you're right - it works fine!
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 4th, 2021, 08:34 AM
#5
Re: Sudden problem with std::vector
Originally Posted by John E
it works fine!
But, it only works if the function itself is executable at compile-time. If not, you still need to use an std::vector.
-
December 4th, 2021, 09:00 AM
#6
Re: Sudden problem with std::vector
Ah yes, you're right - in fact even something as simple as this doesn't work...
Code:
constexpr uint32_t some_func() { return sqrt(1024); }
Last edited by John E; December 4th, 2021 at 09:16 AM.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
December 4th, 2021, 12:08 PM
#7
Re: Sudden problem with std::vector
For a constexpr function to return a constexpr value, any functions it calls must also be marked as constexpr. ie. can be evaluated at compile time. Unfortunately, sqrt() isn't one of them.
See https://en.cppreference.com/w/cpp/language/constexpr
and the example of a compile-time factorial function.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|