template class alias inside other class body produces compilation error - c++

I am working on Visual Studio 2015 community edition.
The following code produces an error:
#include <list>
using std::list;
template <typename T>
class enumerator
{
// typedef:
public:
using list_ = list<T*>; // line:
using list_fwd_c = list_::const_iterator; // 188
using list_fwd = list_::iterator; // 189
using list_rvs_c = list_::const_reverse_iterator; // 190
using list_rvs = list_::reverse_iterator; // 191
}; // 192
and the error:
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2061: syntax error: identifier 'const_iterator'
1> d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(192): note: see reference to class template instantiation 'cpplib::eval::utils::enumerator<T>' being compiled
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(188): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2061: syntax error: identifier 'iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(189): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2061: syntax error: identifier 'const_reverse_iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(190): error C2238: unexpected token(s) preceding ';'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2061: syntax error: identifier 'reverse_iterator'
1>d:\develop\workspace\visual_studio\nevada_test_site\source\shared\cpp\expression_evaluator\expression_evaluator\src\expr_eval.cpp(191): error C2238: unexpected token(s) preceding ';'
I have confirmed this is the troublesome code piece (error disappear when I comment out the 'enumerator' class).
As its name suggests, it "will be" a simple enumerator class (something like a IEnumerator from C#. I will use it in order to store a list of objects derived from a base class passed as a class template parameter T.
Question:
1. Why the code above produces this error.
2. How can I fix this error?
3. I know this is not a place for this, but if you know a good implementation (boost ?) for this kind of enumerator, please point me in the right direction.

You still need to add a typename despite how convenient using is:
using list_fwd_c = typename list_::const_iterator; // 188
using list_fwd = typename list_::iterator; // 189
using list_rvs_c = typename list_::const_reverse_iterator; // 190
using list_rvs = typename list_::reverse_iterator; // 191
The problem is that these are dependent types (they depend on the template parameter) so the compiler cannot just know they are all types (and not, say functions or static data members)

Related

Why doesn't basic_regex support char8_t?

As the title says, the following code snippet generates C2027:
#include<regex>
using namespace std;
int main() {
basic_regex<char8_t> r;
return 0;
}
And the error msg was:
error C2027: use of undefined type 'std::regex_traits<char8_t>'
message : see declaration of 'std::regex_traits<char8_t>'
message : see reference to class template instantiation 'std::basic_regex<char8_t,std::regex_traits<char8_t>>' being compiled
error C2061: syntax error: identifier 'locale_type'
error C2027: use of undefined type 'std::regex_traits<char8_t>'
message : see declaration of 'std::regex_traits<char8_t>'
error C2061: syntax error: identifier 'string_type'
error C3646: 'imbue': unknown override specifier
error C2059: syntax error: '('
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C3646: 'getloc': unknown override specifier
error C2059: syntax error: '('
error C2334: unexpected token(s) preceding '{'; skipping apparent function body
error C2079: 'std::basic_regex<char8_t,std::regex_traits<char8_t>>::_Traits' uses undefined class 'std::regex_traits<char8_t>'
But basic_regex<char> works
Can someone tell me why?
char and char8_t look like the same type in terms of their usage...
I'm using VS2021

constexpr static std::array<const char *,5> failing to compile with MSVC2013

I have this code:
#include <array>
#include <iostream>
class ExternalGeometryExtension
{
public:
enum Flag {
Defining = 0,
Frozen = 1,
Detached = 2,
Missing = 3,
Sync = 4,
NumFlags
};
constexpr static std::array<const char *,NumFlags> flag2str{{ "Defining", "Frozen", "Detached","Missing", "Sync" }};
};
int main()
{
std::cout << ExternalGeometryExtension::flag2str[ExternalGeometryExtension::Frozen] << std::endl;
return 0;
}
It compiles fine with:
clang version 5.0.0 and
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4
It fails to compile with MSVC2013.
The compilation error is:
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2144: syntax error : 'int' should be preceded by ';' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2146: syntax error : missing ';' before identifier 'flag2str' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2838: 'array<char const *,5>' : illegal qualified name in member declaration [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2143: syntax error : missing ';' before '{' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2334: unexpected token(s) preceding '{'; skipping apparent function body [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod\Sketcher\App\ExternalGeometryExtension.cpp(36): error C2143: syntax error : missing ';' before 'std::array<const char *,0x05>' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod\Sketcher\App\ExternalGeometryExtension.cpp(36): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod\Sketcher\App\ExternalGeometryExtension.cpp(36): error C2039: 'flag2str' : is not a member of 'Sketcher::ExternalGeometryExtension' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C2144: syntax error : 'int' should be preceded by ';' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C2146: syntax error : missing ';' before identifier 'flag2str' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C2838: 'array<char const *,5>' : illegal qualified name in member declaration [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C2143: syntax error : missing ';' before '{' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
c:\projects\freecad\src\mod\sketcher\app\ExternalGeometryExtension.h(47): error C2334: unexpected token(s) preceding '{'; skipping apparent function body [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2144: syntax error : 'int' should be preceded by ';' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2146: syntax error : missing ';' before identifier 'flag2str' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2838: 'array<char const *,5>' : illegal qualified name in member declaration [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2143: syntax error : missing ';' before '{' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod/Sketcher/App/ExternalGeometryExtension.h(47): error C2334: unexpected token(s) preceding '{'; skipping apparent function body [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
C:\projects\freecad\src\Mod\Sketcher\App\ExternalGeometryExtensionPyImp.cpp(67): error C2039: 'flag2str' : is not a member of 'Sketcher::ExternalGeometryExtension' [C:\projects\freecad\build\src\Mod\Sketcher\App\Sketcher.vcxproj]
Full compiler output here
My questions are:
What I am doing wrong? Why is it not compiling with MSVC2013?
Is there something I can do to make this code work with MSVC2013 without breaking it in the other compilers?
EDIT: I have changed the code so that it is a Minimal, Complete, and Verifiable example as requested by Toby Speight based on the good guess of Diodacus. I cannot produce the error output of that specific code, because I do not have a copy of MSVC2003. I work on an opensource, FreeCAD, which offers Windows support. I use Linux. In any case, the errors in the output correspond to the code I show. This is the output of the AppVeyor test before integration. The code passes the Linux CI fine. I am going to try to make the most of this question, hopping that it is useful for others.
EDIT 2: I have realised that the double bracket initialization has raised some eyebrows. From the example in cppreference:
double-braces required in C++11 prior to the CWG 1270 revision
(not needed in C++11 after the revision and in C++14 and beyond)
Without double braces gcc 4.8 fails.
As per this microsoft devblog, constexpr is one of the C++11 core language features that is not supported in VS 2013. And it is only partially supported in "Nov 2013 CTP."
Well, the code in question does compile with C++17 option:
#include <array>
#include <iostream>
class ExternalGeometryExtension
{
public:
enum Flag {
Defining = 0,
Frozen = 1,
Detached = 2,
Missing = 3,
Sync = 4,
NumFlags
};
constexpr static std::array<const char *,NumFlags> flag2str{{ "Defining", "Frozen", "Detached","Missing", "Sync" }};
};
int main()
{
std::cout << ExternalGeometryExtension::flag2str[ExternalGeometryExtension::Frozen] << std::endl;
return 0;
}
And there is no need to redeclare static variable outside the class.
#Abdullah Tahiri Please use C++11 constructs (as stated in question labels) or go C++17 for all features. But I am afraid MSVC might be problematic with the code. Is there any special reason you cannot use GCC or CLang on Windows ?

Declaration of templated class member function returning a map

In VC++ 2015 I have an untemplated class which should have a templated memberfunction which returns a map.
Here is a bit of the code:
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
But the compiler throws error messages:
error C2988: Unrecognized template declaration/definition
error C2143: Syntax error: missing ";" before "<"
error C2238: Unexpected Token before ";"
error C2059: Syntax error: "<"
I assume my problem is in having to use a map where the wstring has to be fixed/untemplated but the second argument is my template class.
Of course i followed the compilers suggestions but that didn't get me any further.
You need to #include <map> and add a semi-colon to the end of your class declaration.
As #Kevin and #juanchopanza have pointed out in the comments, you are simply missing a semi-colon and potentially an include. With the following program in VS2013:
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
int main()
{
}
I get an assortment of the errors you listed:
error C2143 : syntax error : missing ';' before '<'
error C2238 : unexpected token(s) preceding ';'
error C2988 : unrecognizable template declaration / definition
error C2059 : syntax error : '<'
And also:
error C2039 : 'map' : is not a member of 'std'
Once I add an include for std::map, the errors are reduced:
#include <map>
class Registry
{
template<class configclass>
std::map<std::wstring, configclass> enumerateSubKeys(std::wstring subKeyName);
}
error C2628 : 'Registry' followed by 'int' is illegal(did you forget a ';' ? )
Which suggests you are missing a semi-colon at the end of the class declaration.

Can't compile boost/any_iterator.hpp in boost 1.57

After (attempting to) upgrade a VS2012 project to use boost 1.57, I can no longer compile--lots and lots of error messages coming out of boost/any_iterator.hpp (see below). As a test, I created a new project that contained nothing but an empty main function and #include "boost/any_iterator.hpp" and got the same set of errors. Here is the code it's complaining about:
// snippet from boost/any_iterator.hpp
template<
class Value
, class Traversal
, class Reference
, class Difference
, class Buffer
>
class postfix_increment_proxy<
range_detail::any_iterator< // line 131
Value
, Traversal
, Reference
, Difference
, Buffer
>
>
{
// ...
};
There is another class in the same file that follows the same pattern and generates identical errors. range_detail::any_iterator is forward-declared a little higher up in the file:
namespace range_detail
{
// ...
template<
class Value
, class Traversal
, class Reference
, class Difference
, class Buffer = any_iterator_default_buffer
>
class any_iterator;
// ...
}
For what it's worth, here's the set of errors I get from VS2012:
Error 1 error C2143: syntax error : missing ';' before '<' [path]\boost\range\detail\any_iterator.hpp 131
Error 2 error C2059: syntax error : '<' [path]\boost\range\detail\any_iterator.hpp 131
Error 3 error C2065: 'Value' : undeclared identifier [path]\boost\range\detail\any_iterator.hpp 134
Error 4 error C2065: 'Traversal' : undeclared identifier [path]\boost\range\detail\any_iterator.hpp 135
Error 5 error C2065: 'Reference' : undeclared identifier [path]\boost\range\detail\any_iterator.hpp 136
Error 6 error C2065: 'Difference' : undeclared identifier [path]\boost\range\detail\any_iterator.hpp 137
Error 7 error C2065: 'Buffer' : undeclared identifier [path]\boost\range\detail\any_iterator.hpp 138
Error 8 error C2923: 'boost::range_detail::any_iterator' : 'Value' is not a valid template type argument for parameter 'Value' [path]\boost\range\detail\any_iterator.hpp 138
Error 9 error C2923: 'boost::range_detail::any_iterator' : 'Traversal' is not a valid template type argument for parameter 'Traversal' [path]\boost\range\detail\any_iterator.hpp 138
Error 10 error C2923: 'boost::range_detail::any_iterator' : 'Reference' is not a valid template type argument for parameter 'Reference' [path]\boost\range\detail\any_iterator.hpp 138
Error 11 error C2923: 'boost::range_detail::any_iterator' : 'Difference' is not a valid template type argument for parameter 'Difference' [path]\boost\range\detail\any_iterator.hpp 138
Error 12 error C2923: 'boost::range_detail::any_iterator' : 'Buffer' is not a valid template type argument for parameter 'Buffer' [path]\boost\range\detail\any_iterator.hpp 138
Error 13 error C2143: syntax error : missing ';' before '{' [path]\boost\range\detail\any_iterator.hpp 140
Error 14 error C2447: '{' : missing function header (old-style formal list?) [path]\boost\range\detail\any_iterator.hpp 140
Is anyone aware of a workaround?
This appears to be a bug in the boost codebase. postfix_increment_proxy and writable_postfix_increment_proxy are both in the boost::iterators::detail namespace (iterator_facade.hpp). However, both names are used unqualified in any_iterator.hpp. Adding boost::iterators::detail:: in front of both names allows the code to compile.
For anyone who's uncomfortable with the idea of editing boost code, including iterator_facade.hpp followed by using namespace boost::iterators::detail followed by an include for any_iterator.hpp will also solve the problem at the cost of namespace pollution. VS2012 doesn't support them so it doesn't do me any good, but you could presumably use a C++11 using too.
Ticket submitted:
https://svn.boost.org/trac/boost/ticket/10754

Why does the order of my #includes matter? (C++)

I've created a header file called "list_dec.h", put it in a folder "C:\Headers", and set my compiler to include files from "C:\Headers", so now I can do things like
#include<list_dec.h>
int main(){return(0);}
but when I try to do something like
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
I get an error (not anything specific, just a huge list of syntax errors in "list_dec.h", which I know aren't real because I've been able to compile it as both a main.cpp file and a .h file in a separate project). However, when I change to order so "list_dec.h" is on top:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
all of the errors go away. So why does the order of the error matter?
NB: As far as I know, this occurs when I use "list_dec.h" with all header files, but the files I'm absolutely positive it occurs in are:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
EDIT: These are the errors I get when "list_dec.h" is below any other header:
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(51) : see reference to class template instantiation 'list<T,limit>' being compiled
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(14) : error C2059: syntax error : ')'
c:\headers\list_dec.h(14) : error C2238: unexpected token(s) preceding ';'
c:\headers\list_dec.h(69) : warning C4346: 'list<T,limit>::{ctor}' : dependent name is not a type
prefix with 'typename' to indicate a type
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(69) : error C2988: unrecognizable template declaration/definition
c:\headers\list_dec.h(69) : error C2059: syntax error : 'constant'
c:\headers\list_dec.h(69) : error C2059: syntax error : ')'
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(79) : error C2143: syntax error : missing ';' before '{'
c:\headers\list_dec.h(79) : error C2447: '{' : missing function header (old-style formal list?)
If it helps, these are the lines mentioned in the errors (14, 69, 78, and 79):
Line 14: list(const T& NULL); (A constructor for "list" class)
Line 69: inline list<T, limit>::list(const T& NULL): (Definition for the constructor, also, the colon at the end is intentional, It part of the definion ie: void x(int n): VAR(n).)
Line 78: inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)
Line 79: { (the begining of the list-copy contructor)
And a lot of people want to see the beginning of "list_dec.h":
template<class T, size_t limit>
class list
NB: These aren't the first lines, but they're where I think the problem is, the lines before them are simply an enumeration called "err".
EDIT: Just a note, "list_dec.h" contains no includes, defines, ifdefs, or anything precede with a '#'. Besides the enumeration, it only contains the "list" class declaration and the "list" class member function definitions.
Generally speaking it should not, however it may be possible for there to be conflicting definitions of symbols or preprocessor macros that end up confusing the compiler. Try to narrow down the size of the problem by removing pieces and includes from the conflicting header until you can see what is causing it.
In response to the error messages you posted, the symbol NULL is often implemented as a preprocessor macro for the number 0. This is so that you can easily use it as a null pointer. Therefore this:
list(const T& NULL);
Could be converted into this syntax error by the preprocessor:
list(const T& 0);
Change the name of the parameter to something other than NULL.
Note that here:
Line 14: list(const T& NULL); (A constructor for "list" class)
NULL is the name of standard macro - when a standard header file is included before list_dec.h it will most likely cause NULL to be defined which will in turn cause your code to look something like this to the compiler:
list(const T& 0);
The constant 0 above makes the line ill-formed C++. You might get more information by instructing your compiler to produce preprocessed output file.
Presumably list_dec.h is running into a macro that's defined in those other headers (or some headers they in turn include) -- hard to say which one without seeing the first error message and the relevant part of list_dec.h!
The actual errors would give a more specific clue, bt it means there's something in your include file that is screwing up the scan for the next one. The most common thing would be some kind of unclude #-directive, like a #if missing its #endif.
If the errors are random in nature, it could be a missing semi colon. The compiler will usually halt on that, but on occasion you get "lucky".
Otherwise, conflicting names or defines. Do you have anything named std for example?