Method argument argument name with method in MSVC C ++

The following snippet does not compile in MSVC C ++ (2015, 2017):

template <typename Type> struct Base : public Type { Base(const Type & type) : Type(type) {} }; struct SomeType { int Type() { return 42; } }; struct Wrong : public Base<SomeType> { Wrong(const SomeType & type) : Base<SomeType>(type) {} }; SomeType some; Wrong wrong(some); 

The compiler is confused and interprets a call to the Type constructor with a call to the Type() method of the class I'm trying to get from. The GNU C ++ compiler has no code problems.

Renaming a template argument. Solve the problem in declaring a base class on something else (not in collision with any method of the base class). Adding something like : (typename Type)(type) doesn't help.

Is this an MSVC C ++ compiler error. Any tips to solve these problems?

+8
c ++ visual-c ++
source share
1 answer

This is an error in MSVC ++. A two-phase search requires Type to be resolved as the name of the template parameter and the base during template definition!

The fact that there is such a member at the time of creation should not interfere. When templates are implemented correctly, it is not as you noted in GCC.

But Microsoft did not implement it correctly until recently . In its implementation, the template behaves more like a macro, which is the cause of the error.

+9
source share

All Articles