void someFunction(SomeClass argument...">

C ++ templates without "typename" or "class"

I use to write patterns like this:

template<typename T>
void someFunction(SomeClass<T> argument);

however - now I came across templates in another thread written as follows:

template<U>
void someFunction(SomeClass<U> argument);

as far as I know, you can use "typename" and "class" interchangably (with the exception of some details regarding nested types ..). but what does it mean if I don't put the keyword in brackets at all?

thank!

thread in question: Writing copy constructor problems for smart pointer

+3
source share
4 answers

This code is incorrect (typo). In this situation should be typenameor class.


, typename/class. , , :

// template <int n>, but n is not used, so we can ignore the name.
template <int>
void foo(std::vector<int>* x) {
}

int main () {
  foo<4>(0);
}

:

typedef int U;

// template <U n>, but n is not used, so we can ignore the name.
template <U>
void foo(std::vector<U>* x) {
}

int main () {
  foo<4>(0);
}

, U typedef .

+12

, , "typename" "class". / , .

+2

, U - . , U typedef, : "class" "typename", .

The one who put "U" was too smart for their own good. So your confusion.

Welcome to the world of preserving dirty codes of other nations.

0
source

If U is a type, than this is a template specialization

0
source

Source: https://habr.com/ru/post/1770959/


All Articles