constructor initializer list does not work when using braces {}

Hi,

I have the following code which compiles with errors in Visual Studio 2015 update 3:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
namespace Iterator_invalidation
    {  
         template<typename T>
         class list_node;
            
         template <typename T>
         class half_list_node {
         public:
             half_list_node( list_node<T>* p) : next{p} {}
             list_node<T> *next;
         };
            
         template <typename T>
         class list_node : public half_list_node<T> {
         public:
             list_node( const T& d, list_node<T>* p)    				
              : half_list_node<T>{ p }, data{ d }
	     {}
             T data;
         };
}


I get the following compiler errors:


main.cpp(1104): note: see reference to class template instantiation 'Iterator_invalidation::list_node<T>' being compiled
main.cpp(1101): error C2334: unexpected token(s) preceding '{'; skipping apparent function body
main.cpp(1102): error C2059: syntax error: '{'
main.cpp(1102): error C2334: unexpected token(s) preceding '{'; skipping apparent function body


This strange error is corrected when the braces are replaced by parenthesis like so:

1
2
3
4
5
6
7
8
 template <typename T>
            class list_node : public half_list_node<T> {
            public:
                list_node( const T& d, list_node<T>* p) 
					: half_list_node<T>( p ), data( d )
				{}
                T data;
            };


What could be wrong?


Regards,
Juan Dent


based on " error C2334", you're using Visual Studio.
I was able to compile your program with VS2017, but in VS2015, I got the same error messages.
It appears dropping the redundant <T> from the base name is another way to work around this bug in VS2015:
1
2
3
		list_node(const T& d, list_node<T>* p)
			: half_list_node{ p }, data{ d }
		{}


Here's the bug report https://connect.microsoft.com/VisualStudio/feedback/details/1088852/visual-studio-fails-to-parse-call-to-base-templated-class-constructor-with-braces
Last edited on
Thanks Cubbi!!
Topic archived. No new replies allowed.