0

I saw this code in a header file.

using pointIndex = typename std::pair<std::vector<double>, size_t>;
  1. Can we use "typename" here without a template?
  2. Is it necessary to use typename here?
  3. What is the difference between "typedef" and "typename"?
  • 1
    Obviously related reading: [Where and why do I have to put the “template” and “typename” keywords?](https://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords) – Some programmer dude Apr 21 '20 at 11:04
  • 1
    One question per question, please. Are you facing some practical/actual problem? If so, what is it? – Asteroids With Wings Apr 21 '20 at 11:04

1 Answers1

4

typename and typedef are completely different things.

typedef is a keyword used to introduce a type alias (a type definition). In recent standards you can also use using for this. So:

typedef int MyThing;

// or
using MyThing = int;

typename is a keyword that says "the next thing is a type". It's used when dealing with templates in some situations, both in template declarations (e.g. template <typename T> void foo() { /*..*/ }) and to help the parser along in some situations. In the example you've given, it is valid, but redundant.

The two things are entirely different, and thus not interchangeable.

Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35