3

the following code produces 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'd remove the A:: qualifier:

constexpr int foo()
{
    return bar();
}

it will be compiled. problem arises when these methods have the same name, and I need to invoke the base class method. (e.g. when using recursive template inheritence)

any workarounds?

3
  • How about this->A::bar();? Commented May 9, 2015 at 12:55
  • @DanielFrey same same Commented May 9, 2015 at 13:19
  • 2
    Have you filed a bug? Commented May 9, 2015 at 16:36

2 Answers 2

2

The actual problem is b is declared as const (constexpr implies const on objects) and you are trying to call non-const (since C++14, constexpr doesn't imply const on methods, see here) method with the const object...

According to the standard, you should not be able to solve the problem by simply removing A:: nor by the static_cast the way you did. Pre-RTM version of Visual Studio 2015 allows you to do this only because its support for constexpr is preliminary and very buggy. C++11 constexpr (but unfortunately not C++14 extended constexpr) expected to be fully supported in the RTM version of VS 2015 (see here).

The correct version of your code is:

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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Found a solution. "this" should be casted to const A*:

struct B : A
{
    constexpr B(){}
    constexpr int foo()
    {
        return static_cast<const A*>(this)->bar();
    }
};

Also works when the methods have the same name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.