C++ Weekly - Ep 251 - constexpr Parameters With C++20's CNTTP

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

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

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

When Jason gets an ICE while trying to demonstrate a c++20 feature, I put a reminder in my calendar to check into this in 6 months.

homomorphic
Автор

I hoped it would be something like this:
auto do_something(constexpr const Vector& vec);

therealchonk
Автор

There's a typo in the opening screen title: "contexpr" vs constexpr ;)

bsdooby
Автор

This would be useful to initialize Microchips. Imagine APIs to initialize your µC. Just pass in the parameters you want and the register values will be calculated at compile time and you could static_assert errors like passing a clock that is higher than the maximum rating.

sirtobi
Автор

For the record, this does compile in MSVC 19.28 with the /std:c++latest flag

warrenbuckley
Автор

Oh yeah this stuff is funky. I've done this a lot in the past, but I try to reduce it because it can get pretty convoluted... at least the way I did it.

Template parameters can only be POD and nothing fancy. Another weird thing that I used for like a year was passing a structure reference as a template parameter (pointers won't work though) and using that to pass the address of a constant (not constexpr in this case) to a function that took that struct. I've since ditched that code in favor of using std::function<> as it solved by problem and made my life easier for my specific application.

If you are wondering why such wizardry was needed, I was doing some embedded systems, I had an interrupt handler that I wanted to be able to take a context. That context came in the way of a constant structure. I needed a function with the signature `void(void)` to become a interrupt service routine. So I made a template function that took the const structs as a template parameter and had them pass the template parameter to the handler with context. Something like this:

```
template <Context & context>
void Handler()
{
HandlerWithContext(context);
};

constexpr Context context1;
constexpr Context context2;
constexpr Context context3;

Handler<context1>(); // This would be how it is used, but if you remove the () you can assign this to a function pointer.
```

I don't recommend this though.

KhalilEstell
Автор

People really need 2 layers for the language. One layer to tell the compiler what to do. The other layer is what we already have.

hanyanglee
Автор

Since you're expressing a requirement that a parameter must be known at compile time, I thought the keyword 'consteval' would come handy. But I guess getting more use of the template syntax isn't so bad

leocelente
Автор

Except, I might want to overload on the constexperness of the parameter.

Omnifarious
Автор

I have bad connection, I just came to press like, I will see later.

RoyBellingan
Автор

I don't understand why the initial example failed. Why wasn't it all properly constexpr?

avjewe
Автор

Mistakenly read CNTTP as CNTPP ~ I was like oh~Something new after RCEP ...

wizardy
Автор

Wouldn't this cause significant code bloat? A new template is instantiated every time the function is called with a different template parameter

Rodrigo-menq
Автор

Not quite what I expected but great nonetheless

SrIgort
Автор

did not know template arguments are only integral types. thanks

nmmm
Автор

note: base class 'std::__pair_base<int, int>' is not public
211 | struct pair`

So it is not valid. Not sure what the standard says about std::pair. Also tried std::tuple with the same error.

milasudril