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

Invalid code in 11.Templates_II Other Notes #183

Open
alarxx opened this issue Feb 26, 2025 · 0 comments
Open

Invalid code in 11.Templates_II Other Notes #183

alarxx opened this issue Feb 26, 2025 · 0 comments

Comments

@alarxx
Copy link

alarxx commented Feb 26, 2025

Image

In first example you can't do that for a function, but you can for a class:

template <int ... IntSeq>
class A {}; // Ok

For functions you can use variadic templates with SFINAE type check or with requires (C++20) or as it is written below with auto, it seems to be called "abbreviated function templates", but specifically as it is written here, it doesn't work that way.

I believe this solution should work below С++20:

#include <type_traits>
template <typename ... TArgs, typename = std::enable_if_t<(std::conjunction_v<std::is_same<TArgs, int>...>)>> // SFINAE
void fun(const TArgs ... args) {}

The first example from slide will generate an error:

template <int ... IntSeq>
void f(IntSeq ... seq){} // Error

int main(){}
g++ main.cpp -Wall -std=c++23 && ./a.out

Output:

main.cpp:2:6: error: variable or field ‘f’ declared void
    2 | void f(IntSeq ... seq){}
      |      ^
main.cpp:2:18: error: expected ‘)’ before ‘seq’
    2 | void f(IntSeq ... seq){}
      |       ~          ^~~~
      |                  )
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