Author Topic: C++ Template meta-progamming - parsing lists of numbers  (Read 4911 times)

0 Members and 1 Guest are viewing this topic.

Offline westfwTopic starter

  • Super Contributor
  • ***
  • Posts: 4672
  • Country: us
C++ Template meta-progamming - parsing lists of numbers
« on: April 03, 2025, 06:36:04 am »
I'd like to pass a C++ template function a string containing a list of numbers, and have it parsed at compile time into an array of bytes that ends up being passed to actual run-time code.
Ie:
Code: [Select]
  set_many_pins("1 10 8 5 2");
  //  calls a worker function
  set_many_pins_worker(5);  // 5 values stored in a static array somewhere.
My knowledge of C++ template programming doesn't go much further than believing that this should be possible...
Any hints or examples?  C++11 would be desirable :-(

 

Offline Tation

  • Frequent Contributor
  • **
  • Posts: 319
  • Country: pt
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #1 on: April 03, 2025, 08:13:26 am »
Wouldn't be constexpr more appropriate for this task? Definitely not an expert here, but used it to create some non-trivial data structures (mainly LUTs) at compile time.
 

Offline brucehoult

  • Super Contributor
  • ***
  • Posts: 6464
  • Country: nz
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #2 on: April 03, 2025, 08:35:48 am »
I'd like to pass a C++ template function a string containing a list of numbers, and have it parsed at compile time into an array of bytes that ends up being passed to actual run-time code.
Ie:
Code: [Select]
  set_many_pins("1 10 8 5 2");
  //  calls a worker function
  set_many_pins_worker(5);  // 5 values stored in a static array somewhere.
My knowledge of C++ template programming doesn't go much further than believing that this should be possible...
Any hints or examples?  C++11 would be desirable :-(

Sir, it is already the 3rd.
 
The following users thanked this post: voltsandjolts

Online tggzzz

  • Super Contributor
  • ***
  • Posts: 23122
  • Country: gb
  • Numbers, not adjectives
    • Having fun doing more, with less
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #3 on: April 03, 2025, 08:36:06 am »
My knowledge of C++ template programming doesn't go much further than believing that this should be possible...

Since the template language is Turing complete, it should indeed be possible to do that. You can, never forget, cause the C++ compiler to emit the never-ending sequence of prime numbers during compilation.

What you should and shouldn't do in the template language is a matter of taste.
There are lies, damned lies, statistics - and ADC/DAC specs.
Glider pilot's aphorism: "there is no substitute for span". Retort: "There is a substitute: skill+imagination. But you can buy span".
Having fun doing more, with less
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17848
  • Country: fr
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #4 on: April 03, 2025, 02:22:10 pm »
You can do that using variadic arguments instead of messing with templates?

If you're allergic to messing with variadic arguments, you can alternatively use std::initializer_list (since C++11).
Unless you prefer templates and strings to lists within curly braces, or absolutely need to use strings at compile-time for a reason I don't know, this is easier, easier to understand and more elegant IMO. Example:

Code: [Select]
#include <cstdint>
#include <initializer_list>

#define PORT ((volatile uint32_t *) 0xABCD1234)

static void set_pin(uint8_t pin)
{
*PORT |= pin;
}

static void set_many_pins(std::initializer_list<uint8_t> pinList)
{
for (uint8_t pin : pinList)
{
set_pin(pin);
}
}

void Foo()
{
set_many_pins({ 1, 10, 8, 5, 2 });
}

(The set_pin() function is obviously hypothetical just to illustrate.)
This is what you get from the above for a Cortex-M4 at -O3:
Code: [Select]
Foo():
        ldr     r3, .L3
        ldr     r2, [r3, #564]
        orr     r2, r2, #1
        str     r2, [r3, #564]
        ldr     r2, [r3, #564]
        orr     r2, r2, #10
        str     r2, [r3, #564]
        ldr     r2, [r3, #564]
        orr     r2, r2, #8
        str     r2, [r3, #564]
        ldr     r2, [r3, #564]
        orr     r2, r2, #5
        str     r2, [r3, #564]
        ldr     r2, [r3, #564]
        orr     r2, r2, #2
        str     r2, [r3, #564]
        bx      lr
.L3:
        .word   -1412624384
Directly inlined.

At -Os, it uses an array and a loop instead:
Code: [Select]
Foo():
        push    {r4, lr}
        ldr     r1, .L4
        ldr     r0, .L4+4
        movs    r3, #6
.L2:
        subs    r3, r3, #1
        bne     .L3
        pop     {r4, pc}
.L3:
        ldr     r4, [r0, #564]
        ldrb    r2, [r1], #1    @ zero_extendqisi2
        orrs    r2, r2, r4
        str     r2, [r0, #564]
        b       .L2
.L4:
        .word   .LANCHOR0
        .word   -1412624384
        .set    .LANCHOR0,. + 0
C.0.0:
        .byte   1
        .byte   10
        .byte   8
        .byte   5
        .byte   2
 

Offline westfwTopic starter

  • Super Contributor
  • ***
  • Posts: 4672
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #5 on: April 04, 2025, 02:42:40 am »
Quote
you can alternatively use std::initializer_list (since C++11).

Thanks!  I think that accomplishes what I want to do.
I had previously found something similar, but it required a more recent version of C++, which I don't have.
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 17848
  • Country: fr
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #6 on: April 04, 2025, 12:48:26 pm »
This above is supported in C++ 11, tested compiling it with 'std=c++11' to be sure - this is what gives the assembly I posted. It's pretty efficient too (I don't think one could get any more efficient) and easy to use.
One of the C++ things that can be "safely" used in embedded dev without a second thought.
 

Offline bson

  • Supporter
  • ****
  • Posts: 2827
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #7 on: July 19, 2025, 09:02:22 pm »
Of course, you can just use an array instead of the initializer_list.

Code: [Select]
static const int pins[]  = { 1, 10, 8, 5, 2 };

set_many_pins(pins, sizeof pins);

That's pretty much all initializer_list is, a reference to an array and its size, with iterators.  It's effectively an immutable vector with fixed underlying storage.

I find these standard library wrappers pretty useless, it's not like it's a challenge to walk an array, it doesn't need an abstraction; nor does an abstraction help readability - but drags in all kinds of abstract silliness like iterators.
 

Offline gf

  • Super Contributor
  • ***
  • Posts: 1840
  • Country: de
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #8 on: July 20, 2025, 12:38:34 pm »
Of course, you can just use an array instead of the initializer_list.

Code: [Select]
static const int pins[]  = { 1, 10, 8, 5, 2 };

set_many_pins(pins, sizeof pins);

That's pretty much all initializer_list is, a reference to an array and its size, with iterators.  It's effectively an immutable vector with fixed underlying storage.

I find these standard library wrappers pretty useless, it's not like it's a challenge to walk an array, it doesn't need an abstraction; nor does an abstraction help readability - but drags in all kinds of abstract silliness like iterators.

The obvious idea is that you can pass {1, 10, 8, 5, 2} directly as an argument, without having to instantiate an array explicitly. The original intended use case was certainly to initialise class objects using syntax similar to that used for arrays or structs, so that you can write directly

Code: [Select]
std::vector<int> v1 = { 1, 10, 8, 5, 2 };

instead of

Code: [Select]
static const int pins[]  = { 1, 10, 8, 5, 2 };
std::vector<int> v2(pins, pins + 5);
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 4874
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #9 on: July 20, 2025, 04:25:48 pm »
Of course, you can just use an array instead of the initializer_list.

Code: [Select]
static const int pins[]  = { 1, 10, 8, 5, 2 };

set_many_pins(pins, sizeof pins);

That's pretty much all initializer_list is, a reference to an array and its size, with iterators.  It's effectively an immutable vector with fixed underlying storage.

By using the provided iterators or list size you don't have to convert sizeof(pins) from byte count to element count.  Or assign to a temporary variable. Or remember to declare the temporary variable static const.  It's not a huge inconvenience but it's a very common use case, and the initializer_list imis basically a compile time construct it doesn't add any runtime overhead.  I don't understand why anyone would object if they are already using C++ in any capacity.

Probably the biggest reason for using initializer_list is when initializing member variables within a constructor.  This makes it much easier to avoid default initialization followed by assignment.
 
The following users thanked this post: SiliconWizard

Offline bson

  • Supporter
  • ****
  • Posts: 2827
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #10 on: July 20, 2025, 08:57:51 pm »
That's pretty much all initializer_list is, a reference to an array and its size, with iterators.  It's effectively an immutable vector with fixed underlying storage.
But only to a constructor.  That's why it's called initializer_list.  The example given is a function call, not a constructor.  So you still need to construct it first, even if as an inline temporary.  And passing initializer_list's to a function call that doesn't actually do initialization is one of those things that scream "just because you can doesn't make it a good idea."
« Last Edit: July 20, 2025, 08:59:43 pm by bson »
 

Offline gf

  • Super Contributor
  • ***
  • Posts: 1840
  • Country: de
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #11 on: July 20, 2025, 09:22:54 pm »
That's pretty much all initializer_list is, a reference to an array and its size, with iterators.  It's effectively an immutable vector with fixed underlying storage.
But only to a constructor.  That's why it's called initializer_list.  The example given is a function call, not a constructor.  So you still need to construct it first, even if as an inline temporary.  And passing initializer_list's to a function call that doesn't actually do initialization is one of those things that scream "just because you can doesn't make it a good idea."

Not only for a constructor. See https://en.cppreference.com/w/cpp/utility/initializer_list.html

Quote
A std::initializer_list object is automatically constructed when:

- a brace-enclosed initializer list is used to list-initialize an object, where the
  corresponding constructor accepts an std::initializer_list parameter,

- a brace-enclosed initializer list is used as the right operand of assignment
  or as a function call argument, and the corresponding assignment
  operator/function accepts an std::initializer_list parameter,

- a brace-enclosed initializer list is bound to auto, including in a ranged for loop.
« Last Edit: July 20, 2025, 09:25:29 pm by gf »
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #12 on: July 21, 2025, 05:35:13 am »
I'd like to pass a C++ template function a string containing a list of numbers, and have it parsed at compile time into an array of bytes that ends up being passed to actual run-time code.
Ie:
Code: [Select]
  set_many_pins("1 10 8 5 2");

If this is intended to be a compile-time construct, why do you want to use a string literal specifically? Is it really a requirement?
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #13 on: July 21, 2025, 05:41:38 am »
Probably the biggest reason for using initializer_list is when initializing member variables within a constructor.  This makes it much easier to avoid default initialization followed by assignment.

??? Um... This strange sentence seems to make no sense (unless it is intended to be some sort of language-lawyer-level joke)

It is indeed very useful to use constructor initializer list to initialize class bases and members in a constructor. This is an important language feature present in C++ since the beginning of times (C++98). However, this feature has absolutely no relation whatsoever to `std::initializer_list<>`. The latter is a completely independent and unrelated feature introduced in C++11.

The names of these features use similar sub-tokens "initializer" and "list", bit otherwise they are completely independent. I don't see why you'd decide to mention constructor initializer lists in a conversation about `std::initializer_list<>`
« Last Edit: July 21, 2025, 05:47:04 am by TheCalligrapher »
 

Offline ejeffrey

  • Super Contributor
  • ***
  • Posts: 4874
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #14 on: July 21, 2025, 02:39:02 pm »

??? Um... This strange sentence seems to make no sense (unless it is intended to be some sort of language-lawyer-level joke)

Language lawyer "jokes" that are annoying, condescending, and don't actually help anyone is more your thing.

In this case, try thinking  before you start insulting people.

The point is: if you have a std::vector or another container as a class member.  If you want to initialize it with a non default value, initializer_list lets you do that with a literal list.  It's the same point as with local variables described above, but withinenmer initialization you can't create named temporaries.
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #15 on: July 21, 2025, 03:29:36 pm »
The point is: if you have a std::vector or another container as a class member.  If you want to initialize it with a non default value, initializer_list lets you do that with a literal list.  It's the same point as with local variables described above, but withinenmer initialization you can't create named temporaries.

(Personal attacks excluded from the quote.)

Well, this is a completely different point. Something as specific as "initialize `std::vector` class member with a literal list" and something as generic as "initialize a class member to avoid double initialization" are two completely different topics. Which is, incidentally, my point :)

Firstly, in your previous message you explicitly mentioned the matter of avoiding double initialization of class members as one key feature in this context (I quote: "...This makes it much easier to avoid default initialization followed by assignment.") But, again, avoiding double initialization is exactly what constructor initializer lists are for. They have always been present in the language and they served that purpose since C++98. And, as I stated above, this has nothing to do with `std::initializer_list<>`.

Secondly, the ability to use list-initialization in constructor initializer lists is indeed new in C++11, however this has nothing to do with `std::initializer_list<>` either. This new ability is a consequence of so-called uniform initialization [syntax]. And uniform initialization has no direct relation to `std::initializer_list<>`. In case of `std::vector` member initialized with a list, `std::initializer_list<>` does indeed serve as an intermediary. In case of `std::array` member initialized with a list, `std::initializer_list<>` plays no role at all.

Moreover, why class members? This ability has absolutely nothing to do with class members. Before C++11 (and its uniform initialization) you could not initialize an `std::vector` with an explicit list anywhere. You could not initialize a local variable like that. You could not initialize a static variable like that. Why are you suddenly talking about class members specifically, if your point is about initializing `std::vector` with literal list?

Thirdly, initial forms of list initialization have been present in C++ since the beginning of times (in fact, it is rooted in good old C). Uniform initialization in large degree is just an extension of that ancient feature. Yet again, `std::initializer_list<>` is still beside the point here.

So, again, why this matter is suddenly pulled into this discussion is still beyond me. That's what I'm trying to figure out, but apparently there's no hope...
« Last Edit: July 21, 2025, 03:39:31 pm by TheCalligrapher »
 

Offline gf

  • Super Contributor
  • ***
  • Posts: 1840
  • Country: de
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #16 on: July 21, 2025, 04:39:00 pm »
Before C++11 (and its uniform initialization) you could not initialize an `std::vector` with an explicit list anywhere.

But now (C++11 or later) you can, and that's where std::initializer_list comes into the play, since a proxy object of type std::initializer_list<T> is used to pass a brace-enclosed initializer-list to one of the constructors of std::vector<T> when you write e.g.

Code: [Select]
std::vector<int> v = { 1,2,3 };

or

Code: [Select]
std::vector<int> v { 1,2,3 };


Under the hood, this constructor overload is invoked:

Code: [Select]
std::vector<int>::vector( std::initializer_list<int> init, const Allocator& alloc = Allocator() );
« Last Edit: July 21, 2025, 05:09:35 pm by gf »
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #17 on: July 21, 2025, 05:38:21 pm »
But now (C++11 or later) you can, and that's where std::initializer_list comes into the play, since a proxy object of type std::initializer_list<T> is used to pass a brace-enclosed initializer-list to one of the constructors of std::vector<T>

That's very nice. But my original issue was with the strange mention of class member and "avoiding assignment after initialization for class members", which is a completely separate matter.

---

On a related note, it is probably worth nothing that brace-enclosed list is not an expression in C++. It has no type. It is not `std::initializer_list<>` and it is not related to `std::initializer_list<>` in any way (contrary to a rather widespread mistaken belief).

Brace-enclosed list is just a grammar-level construct that is parsed as an initializer in a number of [appropriate] contexts. The only connection it has to `std::initializer_list<>` is that it can be used to initialize `std::initializer_list<>`. But brace-enclosed list can be used to initialize many other things as well, not just `std::initializer_list<>`. And thanks to uniform initialization, this in turn can be used to pass brace-enclosed lists around. For example, one can use a classic old naked C-style array to pass a "brace-enclosed initializer-list" to a function

Code: [Select]
template <typename T, std::size_t N> void foo(const T (&a)[N]) { /* ... */ }
...
foo({ 1, 2, 3 });
foo({ "a", "b", "c", "d" });

No `std::initializer_list<>` here, as you can see. One can use `std::array`, `std::tuple`, variadic templates and a number of other language features to achieve the same effect. However, the distinctive property of `std::initializer_list<>` is that it is run-time sized, as opposed to compile-time sized alternatives (e.g. my example above). Run-time sizing is basically the only (!) distinctive property of `std::initializer_list<>`, its raison d'être.

Thanks to `std::initializer_list<>`, I can re-implemenet the above without having `N` as a template parameter

Code: [Select]
template <typename T> void foo(std::initializer_list<T> a) { /* ... */ }
That's about it. That is what `std::initializer_list<>` is actually for: support for convenient short-lived storage of run-time sized sequences of identically typed elements.
« Last Edit: July 21, 2025, 06:04:30 pm by TheCalligrapher »
 
The following users thanked this post: gf

Offline gf

  • Super Contributor
  • ***
  • Posts: 1840
  • Country: de
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #18 on: July 21, 2025, 07:58:13 pm »

Code: [Select]
template <typename T, std::size_t N> void foo(const T (&a)[N]) { /* ... */ }
...
foo({ 1, 2, 3 });
foo({ "a", "b", "c", "d" });

No `std::initializer_list<>` here, as you can see. One can use `std::array`, `std::tuple`, variadic templates ...

I tried the same with std::array instead of a plain array, but it can neither deduce template parameters T + N, nor N alone.
What's wrong with foo2 and foo3?
https://godbolt.org/z/4cj3nP54T
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #19 on: July 21, 2025, 08:11:00 pm »
I tried the same with std::array instead of a plain array, but it can neither deduce template parameters T + N, nor N alone.

I think I got a bit too optimistic about other opportunities. Indeed, `std::array`, `std::tuple` etc. will not be able to deduce template parameters from brace-enclosed initializer list. Only plain C-style array can as well as `std::initializer_list<>`. So, this is another reason for `std::initializer_list<>` to exist: it uses a dedicated set of type deduction rules tailored specifically for it.

Variadic templates will also refuse to accept brace-enclosed initializer list as an argument, although within the context of the original question they still might be useful, just without the `{}` around the list.
 

Offline gf

  • Super Contributor
  • ***
  • Posts: 1840
  • Country: de
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #20 on: July 21, 2025, 08:37:03 pm »
I tried the same with std::array instead of a plain array, but it can neither deduce template parameters T + N, nor N alone.

I think I got a bit too optimistic about other opportunities. Indeed, `std::array`, `std::tuple` etc. will not be able to deduce template parameters from brace-enclosed initializer list.

While deduction did not work in conjunction with a function call, it is interesting that it does work when a std::array variable is initialized: https://godbolt.org/z/TdGMh9M6o
 

Offline TheCalligrapher

  • Regular Contributor
  • *
  • Posts: 193
  • Country: us
Re: C++ Template meta-progamming - parsing lists of numbers
« Reply #21 on: July 21, 2025, 08:58:11 pm »
While deduction did not work in conjunction with a function call, it is interesting that it does work when a std::array variable is initialized: https://godbolt.org/z/TdGMh9M6o

Yes, since C++17 we have template argument deduction for class templates as well (CTAD). However, this process does not naturally pass through function parameters to function template arguments unfortunately, i.e. there's no such thing as implicit "CTAD followed by FTAD" deduction in C++. It appears that the only way to take advantage of CTAD in such cases is to do something like

Code: [Select]
template<typename T, std::size_t N> void foo2(const std::array<T, N> &) { }

int main()
{
  foo2(std::array{ 1, 2, 3, 4 });
}

Note that in this version of the code the template arguments are still "deduced", since we are not supplying them explicitly anywhere. Yet, we are forced to trigger "early deduction" by mentioning `std::array` at the call site, essentially turning it into an explicit "CTAD followed by FTAD" two-step deduction.
« Last Edit: July 23, 2025, 04:54:22 pm by TheCalligrapher »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->