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;
};
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 }
{}