Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Think about Vec construction from variadics #423

Open
danakj opened this issue Dec 6, 2023 · 0 comments
Open

Think about Vec construction from variadics #423

danakj opened this issue Dec 6, 2023 · 0 comments
Labels
design Design of the library systems as a whole, such as concepts

Comments

@danakj
Copy link
Collaborator

danakj commented Dec 6, 2023

Also in https://thephd.dev/sol3-compile-times-binary-sizes, it points out the enormous cost of variadics. But initializer_list is not a good option as it breaks move-only things.

Noting that rust has vec![1, 2, 3] which is a macro to create vectors from arbitrarily long sets of inputs, and it does this without templates/variadics.

Some ideas:

  • Stamp out a ton of ctors like Vec<T>(T), Vec<T>(T, T), Vec<T>(T, T, T) ... etc and just hide all but one from docs. This avoids combinatorial explosion in codegen otherwise caused by passing many different types. But every function would get generated for every Vec right?
  • sus_vec(T, ...) macro that figures out how many args it was given, calls Vec::with_capacity() and then push() for each argument, inside a lambda to keep things in scope and then returned.
    #define sus_vec(T, ...) \
      [&] { \
        constexpr ::sus::num::usize s = SIZE_OF_VAR_ARGS??(__VA_ARGS__); \
        auto v = ::sus::Vec<T>::with_capacity(s); \
        FOR_EACH(PUSH_IT, __VA_ARGS__) \
        return v; \
      }()
    
    #define PUSH_IT(it) v.push(it);
    Something like that.
  • Generate a warning when you call Vec(...) with too many things, by calling some [[warning]] function, and point the caller to a Vec<T>::with_initializer_list(std::initializer_list<T>).
@danakj danakj added the design Design of the library systems as a whole, such as concepts label Dec 6, 2023
@danakj danakj added this to the Containers library milestone Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
design Design of the library systems as a whole, such as concepts
Projects
None yet
Development

No branches or pull requests

1 participant