1

The following code does not compile under G++ 4.8

#include <vector>
using namespace std;

int main() {
    vector<int> v;
    typeof(v)::iterator it;
}

If I replace typeof to decltype, it works fine. I know about a workaround with a template structure

template<class T> struct Self {
    typedef T Type;
};

and then

Self<typeof(v)>::Type::Iterator it;

but still its annoying.

Is this a bug which should be reported? Or this is a feature?

Ali
  • 56,466
  • 29
  • 168
  • 265
user31264
  • 6,557
  • 3
  • 26
  • 40
  • 3
    Now that we have decltype, typeof is as good as dead. Why use it at all? – n. m. could be an AI Apr 17 '14 at 20:29
  • I don't like this word. Typeof sounds better and is shorter by 2 characters – user31264 Apr 18 '14 at 21:18
  • *"I don't like this word. Typeof sounds better and is shorter by 2 characters"* Neither of these is a valid reason to use `typeof`. – Ali Apr 19 '14 at 07:49
  • You asked why, I answered you. The question was not whether I should or should not use typeof, but if there is a bug or not. Also, my code will not be portable to GCC 4.6 and earlier. – user31264 Apr 20 '14 at 06:44

1 Answers1

4

Here I am just writing up n.m.'s comment as an answer and expanding on it a little bit.

In C++11, we have decltype which can be used with ::. Consider the following code:

#include <vector>
using namespace std;

int main() {
    vector<int> v;
    decltype(v)::iterator it;
}

The above code compiles cleanly with g++ -std=c++11 -Wall -Wextra -pedantic typeof.cpp.

Since decltype is standard and already supported by gcc 4.3 (released in 2008, 6 years ago), there is absolutely no reason to use the gcc extension typeof instead. Your code will be standard, hence portable, if you use decltype.

Community
  • 1
  • 1
Ali
  • 56,466
  • 29
  • 168
  • 265
  • decltype is supported by gcc 4.3, but decltype(v)::iterator is supported only since gcc 4.7 or 4.8. – user31264 Apr 18 '14 at 21:17
  • Oh, I did not know that. It is most certainly supported in gcc 4.7 because that is what I used :) And gcc 4.7 is already 2 years old. – Ali Apr 18 '14 at 21:28
  • I wrote in my question "If I replace typeof to decltype, it works fine.", so your answer just repeats what is already in my question. – user31264 Apr 19 '14 at 02:37
  • @user31264 No, sorry, my answer gives you good reasons why you should not use `typeof` in the first place. If you *"don't like"* the word `decltype` or that two extra characters are too painful for you, then I am sorry, nobody can help you here. – Ali Apr 19 '14 at 07:47
  • The reasons are not that good. My code will not be portable to GCC 4.6 or less. – user31264 Apr 20 '14 at 06:45
  • @user31264 Self-contradiction: You are complaining in your question that you cannot write `typeof(v)::iterator it;` with gcc **4.8**; then how does it matter that `decltype(v)::iterator it;` wouldn't work with gcc 4.6 if you are already using gcc 4.8? Then post a new question and complain about gcc 4.6. I bet that question will be closed really quick. – Ali Apr 20 '14 at 09:38