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

Do not use variadic type parameters in templates #6

Open
dangelog opened this issue Sep 8, 2022 · 1 comment
Open

Do not use variadic type parameters in templates #6

dangelog opened this issue Sep 8, 2022 · 1 comment

Comments

@dangelog
Copy link

dangelog commented Sep 8, 2022

A container's type may include non-type template parameters, e.g.

QVarLengthArray<int, 32> container;
                  // ^^ NTTP

Accepting them with template <typename ...> typename Container will therefore fail:

    QVector<int> vec{1, 2, 3, 4};
    const auto toString = [](int i) { return QString::number(i); };
    auto result = KDAlgorithms::transformed<QVarLengthArray>(vec, toString); // ERROR

For some reason this was a deliberate choice. But why simply using template <typename> typename Container doesn't work here?

@dangelog dangelog closed this as completed Sep 8, 2022
@dangelog dangelog reopened this Sep 8, 2022
@dangelog dangelog changed the title In templates, use placeholder types (NTTPs) instead of type parameters when handling containers Do not use variadic type parameters in templates Sep 8, 2022
@dangelog
Copy link
Author

dangelog commented Sep 9, 2022

Further investigation shows that template <template <typename> class X> can match something like e.g. std::vector only since C++17:

https://cplusplus.github.io/CWG/issues/150.html
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2016/p0522r0.html

The suggestion therefore would be to have

  • the variadic version in C++14
  • the non-variadic one in C++17

maybe using a macro or so.

Possible cross-test:

// C++17 only, since QVLA has a non-type template parameter
void TestAlgorithms::transformedChangeContainerAndDataType3()
{
    QVector<int> vec{1, 2, 3, 4};
    const auto toString = [](int i) { return QString::number(i); };
    auto result = KDAlgorithms::transformed<QVarLengthArray>(vec, toString);
    QVarLengthArray<QString> expected{"1", "2", "3", "4"};
    QCOMPARE(result, expected);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant