Skip to content

Instantly share code, notes, and snippets.

@DigiTec
Created February 15, 2016 21:31
Show Gist options
  • Save DigiTec/f22e57ca3c508b558697 to your computer and use it in GitHub Desktop.
Save DigiTec/f22e57ca3c508b558697 to your computer and use it in GitHub Desktop.
Maybe a loophole/compiler bug in VS 2015 update 1 around static constexpr and embedding classes with vftable pointers
// Const Expr Playground.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
typedef void(*InitializeMethod)(int foo);
void InitMethod(int foo)
{
}
class IFoo
{
public:
virtual void Method1() const = 0;
virtual void Method2() const = 0;
};
class Proto : public IFoo
{
public:
constexpr Proto(InitializeMethod meth, bool foo) : m_meth(meth), m_foo(foo)
{
}
void Method1() const override { printf("%d", m_foo); }
void Method2() const override {}
private:
InitializeMethod m_meth;
bool m_foo;
};
struct ArrayOfStuff
{
Proto m_proto;
bool m_foo;
};
int main()
{
static constexpr ArrayOfStuff m_foos[] = {
{ { InitMethod, true }, true },
{ { InitMethod, false }, false },
};
static_cast<const IFoo*>(&m_foos[0].m_proto)->Method1();
m_foos[1].m_proto.Method1();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment