0

I'm trying to compile this c++ code

template<int I> using Foo = int;
using Bar = int;

and getting these compiler errors (with g++ -c test.cpp)

test.cpp:1:17: error: expected unqualified-id before ‘using’
 template<int I> using Foo = int;
test.cpp:2:7: error: expected nested-name-specifier before ‘Bar’
 using Bar = int;
Speed8ump
  • 1,307
  • 10
  • 25
  • 3
    This is a not a very good self answer question as you provide not enough information for anyone but you to know what the answer is. You should have an [mcve] and show how it is compiled. – NathanOliver Jan 25 '16 at 14:27
  • Edited to provide a complete, minimal, and compileable example. I actually think the specificity is less useful to anyone following along trying to identify a root cause of their issue...but I guess that's the rules. – Speed8ump Jan 25 '16 at 16:54

1 Answers1

0

My problem was a syntax error, as most solutions for this error suggest, just not any of the typical ones.

The newer (as of c++11) type alias forms of "using" also report this error when you try to compile them using older standards revisions. Add the g++ option -std=c++11 (or one of the later standards options) to let g++ know it needs to compile your code using c++11 features.

Speed8ump
  • 1,307
  • 10
  • 25
  • 8
    I'm not sure a self-answered Q&A that says "turn on C++11 to use C++11" was really warranted. – Lightness Races in Orbit Jan 25 '16 at 14:25
  • I searched for almost an hour trying to figure out why this error was being reported only to discover that an option I thought was turned on wasn't the default. Go search. Every answer to "why am I getting this error" is "you're missing a semicolon" or "you have a syntax error in some header file". The root cause of the error isn't apparent from the error text or any other answered (or from what I could tell asked) question. – Speed8ump Jan 25 '16 at 15:42
  • 1
    That's because it's a minor failure to read the documentation which does not make a good question here. Such questions are routinely closed. – Lightness Races in Orbit Jan 25 '16 at 15:44
  • I'd say the point was more that "type aliasing is a c++11 feature", which I didn't know since I hardly ever even use templates. It can only be a duplicate of the indicated answer if you already know it's a c++11 feature. I'm not going to reopen it, but it isn't a duplicate of that question. – Speed8ump Jan 25 '16 at 17:21
  • The first result on Google for "C++ type alias" is the cppreference.com documentation for C++ type aliases, which has "**(since C++11)**" in big bold letters at the top of the page. We don't do "I didn't read the documentation" questions here. – Lightness Races in Orbit Jan 25 '16 at 17:49
  • (However, I appreciate and commend your intent.) – Lightness Races in Orbit Jan 25 '16 at 17:50
  • Search using the compiler error string says it's a syntax problem so I'm looking for syntax problems. I didn't originally know it was called type aliasing, but I got the cppreference.com via `c++ using template` and probably ended up on the same page (primed to look for syntax errors & ignoring all else apparently). Also, would you have marked this question as a duplicate of that question if I *hadn't* provided the answer? Maybe if I hadn't included c++11 in the tags since that's not part of the question but part of the answer? I'm just confused by the duplicate tag. – Speed8ump Jan 25 '16 at 19:09
  • _"Also, would you have marked this question as a duplicate of that question if I hadn't provided the answer"_ Yes – Lightness Races in Orbit Jan 25 '16 at 20:48