C++ Weekly - Ep 269 - How To Use C++20's constexpr std::vector and std::string

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

Upcoming Workshop: C++ Best Practices, NDC TechTown, Sept 9-10, 2024
Upcoming Workshop: Applied constexpr: The Power of Compile-Time Resources, C++ Under The Sea, October 10, 2024

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

Note, there is a bug in the example at 2:50

Here is a better example, not that it compiles for "false" and fails to compile for "true" because it executes a branch that is not constexpr, which is the kind of thing I was going for.

cppweekly
Автор

Is someone already making a compile-time compiler?

therealchonk
Автор

Will we eventually see a compile time debugger with breakpoints, step, next etc?

paulfee
Автор

This compile-time runtime duality made me reminisce about boost fusion... ah that was so much fun....

pieterodb
Автор

Compile time/runtime reminds me of Forth. A beautifully simple 'close to the metal' programming language based on stacks and RPN. +1

NotMarkKnopfler
Автор

Calling begin() and end() without std:: sort of blows my mind. :O I'm not used to seeing argument-dependent lookup except in operators.

X_Baron
Автор

For the get_vector().size(); failure, could it be the case that to be able to call size() the compiler is creating a temporary instance of std::vector<int> and that is being seen as an instance of moving compile time allocated memory to the runtime world, leading to that particular failure?

sukraatahluwalia
Автор

I guess that vectors are going to be returnable in the far future. The contents might be stored to static memory and when there was a call to the function, a real vector with heap memory is created in the similar way like from an initializer list

cmdlp
Автор

When VS 2019 16.10 Preview 3 will be released you can test std::format. Or earlier if you have time to compile Microsoft stl from github.

igorzhukov
Автор

Can you write the Ackermann function as a constexpr? :-)

thomaslehner
Автор

Ah haa - It's the extension "Font Sizer 2.0" fork by Mads Kristensen(vs2019) (originally by Craig R. Eddy(vs2017)) that you've added to a toolbar!!! Even got the typo.
Thanks!
Original intended post:
01:19 "...those handy little fontsizer button...". "Increase/Decrease Environment Font Size". How/Where did you find those? An extension?
Could only find the Editor font "Zoom in/out" and those where without icons.
Please share. Really miss that functionality in VS proper (VS Code has it).

PS! If you've named the buttons yourself there is a small typo that I also constantly make. s/Enviornment/Environment/

siggimund
Автор

This video title promised constexpr std::string usage. Did I miss it?

dksmiffs
Автор

I sort of thought we would be allowed to make empty vectors ve constexpr. The use case I thought about was doing loookup and returning a reference to a vector, but if no element is found, you can still return a reference to a statically allocated vector. You sort of also want that created at compile time, because you should know what a const empty vector looks like at compile time.

However, I guess std::span is going to fulfil that role even better.

Tyranisaur
Автор

How can you remove the whole bool use_vector parameter with one keystroke? Is it an out of the box VS feature?

Lojdika
Автор

Interesting, potentially very powerful and useful and of course overly complicated and counter-intuitive to use... C++ never really changes it only gets more C++! Seriously though do you think that in the future standards we could see some kind of automatic memory context management when it comes to compile / runtime sharing? It's obviously possible as you've just shown, just that your compile time results are dependent on another compile time expression.. it's almost like we need another level of pre-compile time computation to compute the order of the compile time expressions..!?

fredhair
Автор

Couldn't you have an auto return type and return a std::array constructed in the get_vector function?

ruler
Автор

Why do you use auto keyword for every function? Is it modified in compile time?

JinskuKripta
Автор

This is great but why is no one worried about compile times being increased in the process when using stuff like this? I can imagine in big projects the compile times already are long and then you add this on top. How much the compile time would be increased in percentage if you have to evaluate much more code in compile time every time you are developing a big application?

Kazooie
Автор

Why not use `constexpr auto vector = get_vector()` in constexpr function? then you could use size for array arg.

von_nobody
Автор

Why not just get rid of of the template parameter size, and on line 25 just use std::array<int, vector.size()> result{}; isnt that the same thing just everything is happy in constexpr land?

gnautist