mercredi 1 avril 2015

Inline member initializer containing pointer to member

At work, I'm experimenting a bit to bring some reflection into our codebase. Basically what I want to achieve, is to capture a pointer to data-member inside the type of the data-member's initializer:



template<class Class, int Class::*dataMember>
struct Reflect
{
operator int() {return 0;}
};

class Foo
{
public:
int bar = Reflect<Foo, &Foo::bar>{};
};


Although clang 3.4.1 (http://gcc.godbolt.org/) and Intel C++ XE 14.0 are able to compile this piece of code, when using MSVC12 I get the following error message:



error C2065: 'bar' : undeclared identifier


error C2975: 'dataMember' : invalid template argument for 'Reflect', expected compile-time constant expression



Furthermore, gcc 4.9.2 also seems to have trouble with it: http://ift.tt/1HiQ3ls.


So my questions are:



  1. Is the above piece of code valid C++11?

  2. If yes, are there any work arounds for the failing compilers?


Aucun commentaire:

Enregistrer un commentaire