Internal VS2015 compiler error when calling constexpr base class method
the following code creates an internal compiler error (VS2015)
struct A
{
constexpr A(){}
constexpr int bar()
{
return 3;
}
};
struct B : A
{
constexpr B(){}
constexpr int foo()
{
return A::bar();
}
};
int main()
{
constexpr B b;
constexpr int dummy = b.foo();
return 1;
}
However, if I remove the classifier A:
constexpr int foo()
{
return bar();
}
it will compile. the problem occurs when these methods have the same name and I need to call a method of the base class. (for example when using recursive template inheritance)
any workarounds?
source to share
The actual problem is b
declared as const
( constexpr
implies const
on objects) and you are trying to call a non const
(since C ++ 14, constexpr
doesn't imply const
on methods, see here ) a method with an object const
...
According to the standard, you won't be able to fix the problem simply by deleting A::
or the static_cast
way you did it. The pre-RTM version of the RTM only allows you to do this because its support constexpr
is preliminary and very poor. C ++ 11 constexpr
(but unfortunately not C ++ 14 extended constexpr
), which should be fully supported in the RTM version of VS 2015 (see here ).
Correct version of your code:
struct A
{
constexpr A(){}
constexpr int bar() const
{
return 3;
}
};
struct B : A
{
constexpr B(){}
constexpr int foo() const
{
return A::bar();
}
};
int main()
{
constexpr B b;
constexpr int dummy = b.foo();
return 1;
}
source to share