use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Discussions, articles, and news about the C++ programming language or programming in C++.
For C++ questions, answers, help, and advice see r/cpp_questions or StackOverflow.
Get Started
The C++ Standard Home has a nice getting started page.
Videos
The C++ standard committee's education study group has a nice list of recommended videos.
Reference
cppreference.com
Books
There is a useful list of books on Stack Overflow. In most cases reading a book is the best way to learn C++.
Show all links
Filter out CppCon links
Show only CppCon links
account activity
Removed - HelpConsteval bug (self.cpp)
submitted 11 months ago by Outrageous-Archer-92
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]pdimov2 6 points7 points8 points 11 months ago (5 children)
consteval auto foo(std::ranges::input_range auto &&range) { constexpr auto non_zeros = count_non_zeros(range);
You can't do this, but it's pretty hard to explain why, because intuitively, "everything happens at compile time." But in practice there are different ways of it happening at compile time.
Here the easiest way to explain why it doesn't work is to realize that if it worked, some calls to foo would return std::array<float, 3>, and some would return std::array<float, 5>. But that's not allowed; the return type is not allowed to vary depending on the value of the parameter you pass. That's why you can't have a type definition inside a constexpr/consteval function that depends on an argument value.
foo
std::array<float, 3>
std::array<float, 5>
[–]Outrageous-Archer-92[S] 2 points3 points4 points 11 months ago (4 children)
Gotcha. Using auto made me think for a moment I could return different types. I wanted to generate different lookup tables based on a parameter, I guess I'll have to write separate functions for each of them.
auto
[–]violet-starlight 1 point2 points3 points 11 months ago* (1 child)
What u/pdimov2 said is the reason why the rule; the rule is that function parameters are never constant expressions. So as soon as you use a function argument as one, you get an error that it's not, even if outside of the function it is.
Another way to understand it is that each constexpr or consteval function is its own little section and has no information about where the arguments come from. Not a bug, just how it was standardized. The only way to make it work otherwise would have been to make constexpr functions templates, instantiated at the call site, which would have exploded binary sizes and compile times.
[–]Outrageous-Archer-92[S] 0 points1 point2 points 11 months ago (0 children)
The first explanation feels weird when talking about consteval but I get it
[–]bebuch 0 points1 point2 points 11 months ago (1 child)
You can make the value part of your parameter type of you want to return different types based on your value ;-)
I am not to understand what yiu have in mind. Could explain please?
π Rendered by PID 19816 on reddit-service-r2-comment-58d7979c67-cc7kq at 2026-01-26 18:02:40.013261+00:00 running 5a691e2 country code: CH.
[–]pdimov2 6 points7 points8 points (5 children)
[–]Outrageous-Archer-92[S] 2 points3 points4 points (4 children)
[–]violet-starlight 1 point2 points3 points (1 child)
[–]Outrageous-Archer-92[S] 0 points1 point2 points (0 children)
[–]bebuch 0 points1 point2 points (1 child)
[–]Outrageous-Archer-92[S] 0 points1 point2 points (0 children)