The following small example of C++20 code gives a segmentation fault when run. Why?
If I create an object of class Implementation and call the consteval function implementation.foo() it returns 42 as expected. However if I create a reference of type Interface and call the consteval function interface.foo() I get a segmentation fault. I am missing something in why this would happen.
// Compile with
//
// g++ -std=c++20 -Werror -Wall -Wextra -Wpedantic consteval.cpp -o consteval
#include <iostream>
struct Interface
{
virtual consteval int foo(void) = 0;
};
struct Implementation final : public Interface
{
consteval int foo(void) override { return 42; }
};
int main(void)
{
Implementation implementation;
std::cout << "implementation.foo() returns: " << implementation.foo() << std::endl;
Interface& interface = implementation;
std::cout << "interface.foo() returns: " << interface.foo() << std::endl;
return 0;
}
Link to Compiler Explorer