Note that there are some explanatory texts on larger screens.

plurals
  1. POPointer to member, within class
    primarykey
    data
    text
    <p>I am trying create a typedef for a template that needs a pointer to member, from within a class. The best way to describe it is to show the minimal sample code:</p> <pre><code>template&lt;typename T, int T::*MV&gt; struct Bar { const int &amp;value(const T &amp;t) const { return (t.*MV); } }; struct Foo { int baz; typedef Bar&lt;Foo, &amp;Foo::baz&gt; GetBaz; // Compiler error }; typedef Bar&lt;Foo, &amp;Foo::baz&gt; GetFooBaz; // Compiles just fine int main(int argc, char* argv[]) { Foo f = { 42 }; Foo::GetBaz b; // Fails to compile GetFooBaz b2; // Fine int val = b.value(f); // Fails to compile because of above int val2 = b2.value(f); // Fine } </code></pre> <p>I am not necessarily dead-set on getting access to a member pointer in this way, I would be fine just knowing the offset to the variable and having the Bar::value function perform trickery.</p> <p>As a last resort, I suppose I could use a traits class since the would move the definition outside of the class, but I would prefer to be able to declare the typedef near the variable being used.</p> <p>And, to answer the "why would you want to do that" question, this is all for an IoC container that closely represents the way MEF (C# works).</p> <p>The specific compiler I am using is VC12, but it would be nice if VC11 supported it as well.</p> <p>Thanks</p> <p>EDIT:</p> <p>Here are the error messages</p> <p>1>------ Build started: Project: MemVarBug, Configuration: Debug Win32 ------ 1> MemVarBug.cpp 1>memvarbug.cpp(20): error C2327: 'Foo::baz' : is not a type name, static, or enumerator 1>memvarbug.cpp(20): error C2065: 'baz' : undeclared identifier 1>memvarbug.cpp(20): error C2975: 'MV' : invalid template argument for 'Bar', expected compile-time constant expression 1> memvarbug.cpp(7) : see declaration of 'MV' ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload