BUG: C2964 On Pointer-To-Member As Template Argument

Article translations Article translations
Article ID: 249045
This article has been archived. It is offered "as is" and will no longer be updated.
This article has been archived. It is offered "as is" and will no longer be updated.
Expand all | Collapse all

Symptoms

The compiler generates an error such as the following when a pointer-to-member is used as a class template parameter.
error C2964: invalid expression as template parameter

Cause

This C++ language feature has not yet been implemented in Visual C++.

Resolution

To not use the pointer-to-member as a template argument, pass it as a parameter to the class constructor.

Status

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

More information

Use the following sample code to reproduce the behavior:
#ifdef BUG

struct MyClass {
   char a,b;
   MyClass() : a('a'), b('b') {};
};

template<char MyClass::*member_ptr> 
class AnotherClass {
private:
   MyClass m_var;
public:
   char value() { return m_var.*member_ptr; }
};

int main()
{
   AnotherClass<&MyClass::a> var1;
   AnotherClass<&MyClass::b> var2;	//<<<< Causes compiler error.
   return 0;
} 

#else

// WORKAROUND
struct MyClass {
   char a,b;
   MyClass() : a('a'), b('b') {};
};

template<typename MbrType, typename ClassType> 
class AnotherClass {
private:
   MyClass m_var;
   MbrType ClassType::*member_ptr;
public:
   AnotherClass(MbrType ClassType::*mem_ptr) : member_ptr(mem_ptr) {}
   char value() { return m_var.*member_ptr; }
};

int main()
{
   AnotherClass<char,MyClass> var1(&MyClass::a);
   AnotherClass<char,MyClass> var2(&MyClass::b);
   return 0;
} 

#endif
				

Properties

Article ID: 249045 - Last Review: January 10, 2015 - Revision: 2.0
Keywords: 
kbnosurvey kbarchive kbbug kbcompiler kbcpponly kblangcpp kbpending kbtemplate KB249045

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com