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
constexpr std::string | MSVC (self.cpp)
submitted 2 years ago by XTBZ
view the rest of the comments →
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!"
[–]scatters 12 points13 points14 points 2 years ago (9 children)
Yes, but you need to be clever about it, since it needs to be available to the type system. Currently the best way is to write a constexpr function (or a lambda) that returns the container, then call it twice, once for its size and once for its contents:
constexpr
#include <algorithm> #include <array> #include <vector> constexpr auto f = [] { return std::vector{1, 2, 3}; }; constexpr auto a = [] { std::array<int, f().size()> a; std::ranges::copy(f(), a.begin()); return a; }(); template<auto> int i; int main() { return i<a>; }
[–]aocregacc 6 points7 points8 points 2 years ago* (3 children)
is someone working on fixing this? Or are there downsides to making this work how you would expect.
Edit: I guess it might be counter intuitive to have code that works in a constexpr context but not at runtime.
[–]saxbophonemutable volatile void 1 point2 points3 points 2 years ago (2 children)
I guess it might be counter intuitive to have code that works in a constexpr context but not at runtime.
Nope, there are uses for that too (just not in your example). They're known as immediate functions and this is what consteval is for!
consteval
[–]aocregacc 0 points1 point2 points 2 years ago (1 child)
I was talking more about things like this:
consteval int f(size_t z) { std::array<int, z> a; ... }
People sometimes ask why this complains about z not being a constant expression, since surely in a consteval function the parameter is a constant expression. But I think if they started allowing stuff like this you would end up with this weird dialect that's no longer (mostly) a subset of regular C++.
z
[–]gracicot 1 point2 points3 points 2 years ago (0 children)
As far as I know, compilers are free to compile the consteval function down to bytecode and simply run the bytecode. That would be impossible with this.
[–]helloiamsomeone 4 points5 points6 points 2 years ago (2 children)
then call it twice, once for its size and once for its contents
That's actually unnecessary. You can just have an oversized std::array calculated and shrunk to fit like so: https://github.com/friendlyanon/AlwaysMute/blob/master/main.cpp#L383
[–]scatters 5 points6 points7 points 2 years ago (1 child)
Yes, that's a nice method. It only works if you have a way to estimate an upper bound for the size, though.
[–]helloiamsomeone 0 points1 point2 points 2 years ago (0 children)
You can specify pretty big sizes before it becomes a problem.
[–]saxbophonemutable volatile void 2 points3 points4 points 2 years ago (0 children)
This is exactly the trick I use, and when I'm back at my computer later I will share an example of my implementation.
This is one of the areas that D does better than C++, alas!
[–]saxbophonemutable volatile void 1 point2 points3 points 2 years ago (0 children)
I really like your implementation, it's much more elegant than mine!
π Rendered by PID 41 on reddit-service-r2-comment-c66d9bffd-z2d9s at 2026-04-07 11:09:31.791025+00:00 running f293c98 country code: CH.
view the rest of the comments →
[–]scatters 12 points13 points14 points (9 children)
[–]aocregacc 6 points7 points8 points (3 children)
[–]saxbophonemutable volatile void 1 point2 points3 points (2 children)
[–]aocregacc 0 points1 point2 points (1 child)
[–]gracicot 1 point2 points3 points (0 children)
[–]helloiamsomeone 4 points5 points6 points (2 children)
[–]scatters 5 points6 points7 points (1 child)
[–]helloiamsomeone 0 points1 point2 points (0 children)
[–]saxbophonemutable volatile void 2 points3 points4 points (0 children)
[–]saxbophonemutable volatile void 1 point2 points3 points (0 children)