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

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

C++ Weekly With Jason Turner

3 года назад

20,525 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@mkrxyz6493
@mkrxyz6493 - 14.03.2022 22:46

the only true compile-time optimization is performed with meta-template-programming
constexpr std::string in its current form is utter useless garbage
also I don't see it in this video at all

Ответить
@romeklomek5646
@romeklomek5646 - 01.11.2021 15:19

What on earth is happening with people's speech?! Exshtremely? Do you also say conshtexpression?

Ответить
@dksmiffs
@dksmiffs - 25.06.2021 02:05

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

Ответить
@NotMarkKnopfler
@NotMarkKnopfler - 05.05.2021 16:16

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

Ответить
@thomaslehner5605
@thomaslehner5605 - 05.05.2021 11:53

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

Ответить
@ruler5011
@ruler5011 - 05.05.2021 03:06

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

Ответить
@Lojdika
@Lojdika - 03.05.2021 11:58

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

Ответить
@spiceEx
@spiceEx - 29.04.2021 20:57

Hello, what is your visual studio theme

Ответить
@Tyranisaur
@Tyranisaur - 27.04.2021 19:59

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.

Ответить
@stephenhowe4107
@stephenhowe4107 - 27.04.2021 13:10

I want a constexpr program.

It accepts no input and gives no output.
It does not change state in any way whatsoever.
It runs for a while, does nothing, then terminates.
And want an alternative version that does not terminate.

These are useful programs.

Ответить
@Kazooie
@Kazooie - 27.04.2021 11:47

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?

Ответить
@OmarChida
@OmarChida - 27.04.2021 06:08

Does this mean that we can get a compile time segmentation faults 😂

Ответить
@6754bettkitty
@6754bettkitty - 27.04.2021 05:53

too nice!

Ответить
@X_Baron
@X_Baron - 27.04.2021 03:00

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

Ответить
@cmdlp4178
@cmdlp4178 - 26.04.2021 23:02

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

Ответить
@sukraatahluwalia5137
@sukraatahluwalia5137 - 26.04.2021 22:00

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?

Ответить
@pieterodb2302
@pieterodb2302 - 26.04.2021 21:54

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

Ответить
@von_nobody
@von_nobody - 26.04.2021 21:40

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

Ответить
@igorzhukov8687
@igorzhukov8687 - 26.04.2021 21:37

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.

Ответить
@fredhair
@fredhair - 26.04.2021 21:09

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..!?

Ответить
@Omnifarious0
@Omnifarious0 - 26.04.2021 21:01

template <typename T>
auto constexpr array_from_vector(vector<T> const &v) {
return ::std::array<T, v.size()>{v.begin(), v.end()};
}

Ответить
@gnautist
@gnautist - 26.04.2021 20:55

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?

Ответить
@paulfee9839
@paulfee9839 - 26.04.2021 20:35

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

Ответить
@JinskuKripta
@JinskuKripta - 26.04.2021 20:16

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

Ответить
@therealchonk
@therealchonk - 26.04.2021 20:11

Is someone already making a compile-time compiler?

Ответить