1

Is there anyway to identify missing typename in VS ? Does VS at least produce some kind of warning ?

template<class T> 
void dum() {
  std::vector<T> dum_vector;
  typename std::vector<T>::iterator it = dum_vector.begin(); 
  // VS compiles it with or without typename, but I would like to know whether 
  // I forgot to put a typename, since without typename the code may not compile 
  // with another compiler (e.g. GCC)
}
  • 2
    "since without typename the code may not compile with another compiler" => "since without the typename the code is not well formed C++". – juanchopanza Feb 18 '13 at 15:56
  • What version of VS are we talking about? – fredoverflow Feb 18 '13 at 16:00
  • 5
    @FredOverflow How is this a duplicate? This question is not asking what the keywords are for or where they apply, but how to get MSVC to step down from its throne-of-uniqueness and at least *mention* when it's accepting nonstandard syntax. – Angew is no longer proud of SO Feb 18 '13 at 16:04
  • @FredOverflow it is not a duplicate IMO, since I would like to know whether I forgot typename (and template) somewhere in the code. (I mean does VS somehow can be configured to provide a warning etc.) – user2083875 Feb 18 '13 at 16:05
  • @juanchopanza I agree with you, but pragmatic result of not well formed C++ (not obeying standards) = not compiling on another platform. – user2083875 Feb 18 '13 at 16:07
  • 1
    @Angew Identifying missing `typename`s by hand is possible if you know the rules ;) – fredoverflow Feb 18 '13 at 16:08
  • @FredOverflow: What if you forget, and the compiler accepts the code? – Nawaz Feb 18 '13 at 16:11
  • The exact version of visual studio you want to coerce into telling you about the error might be helpful. – Yakk - Adam Nevraumont Feb 18 '13 at 16:11
  • 1
    @FredOverflow It is also possible to write bug-free code, yet few people manage ;-) That's why we have compilers give us warnings as well as errors. – Angew is no longer proud of SO Feb 18 '13 at 16:17
  • I guess if you compiled your code with GCC, then you will get compile-time error, and it will specify the lines where you must use `typename`, since GCC support the two step compilation process on template, whereas MSVC doesn't. – AlexDan Feb 18 '13 at 16:17
  • @AlexDan you are right, I can use GCC for that purpose, but what I am asking can I achieve same thing without using another compiler by only relying on VS. – user2083875 Feb 18 '13 at 16:23
  • @Angew Also, closing as "duplicate" is just so much easier than providing a related link by hand, and sometimes laziness wins... ;) – fredoverflow Feb 18 '13 at 16:43

2 Answers2

1

Actually in the current version of C++ (which is C++11), you don't need to write that much. You just could write this:

auto it = dum_vector.begin(); 

and you're done.

Note that auto is supported since MSVC10, so if you're using it, I would recommend you to use auto in place of blah::blah::iterator. If you're using older version, it is better to upgrade and avail benefits of C++11 features as much as possible. If you cannot do that, then it is very unlikely that MSVS can tell you the missing typename, given that the compiler compiles the non-Standard code in the first place!

Nawaz
  • 353,942
  • 115
  • 666
  • 851
  • 1
    Thanks, although I am very grateful about auto in C++11, current project that I work on haven't switched to C++11 compatible compiler yet. – user2083875 Feb 18 '13 at 16:11
  • Also thanks for additional note, and I also agree with that. If there is no other answers to my question I am going to accept your answer (since you try to explain why such support might not be provided). – user2083875 Feb 18 '13 at 16:27
1

I'm not sure if it has 100% standard conformance, but MSVC produces Compiler Warning (level 1) C4346 for all or most cases in which typename was explicitly needed. So as long as you are compiling with compiler flag /W1 or greater you should be okay.

eladidan
  • 2,634
  • 2
  • 26
  • 39