error C2244 on templated member variable with templated argument function, only happens on Visual St

I ran into an interesting, yet extremely annoying, bug on Visual Studio, below is the simplest repro: (uncomment the #define will allow VS to build the code)

#include <iostream> using namespace std; //#define BUILD_ON_VS class CC { public: template<typename T> struct Foo { template<T foo> void bar() { cout << "VC likes this!\n"; } #ifndef BUILD_ON_VS template<T foo> void bar1(); #endif }; Foo<int> m_foo; }; #ifndef BUILD_ON_VS template<typename T> template<T foo> void CC::Foo<T>::bar1() { cout << "VC doesn't like this...\n"; } #endif int main() { CC cc; cc.m_foo.bar<-1>(); #ifndef BUILD_ON_VS cc.m_foo.bar1<2>(); #endif return 0; }

Basically, I cannot put the definition of the function bar outside of the class in Visual Studio. bar and bar1 are exactly the same otherwise. Test on VS 2010 and VS 2012, both failed with errors:

error C2244: 'CC::Foo<T>::bar1' : unable to match function definition to an existing declaration definition 'void CC::Foo<T>::bar1(void)' existing declarations 'void CC::Foo<T>::bar1(void

It, however, works on all online compilers, such as compileonline and ideone.

I want to keep everything inside the cpp file, not in the .h to keep the code base clean.

Setting var1 to: { template<typename TT, TT foo> void bar1(); } template<typename T> template<typename TT, TT foo> void CC::Foo<T>::bar1() { }

Also works, but it makes the code looks idiotic by redefining the same template parameter and much more prone to bugs. It also makes the interface messy.


Found a fix by typing randomly and see if it compiles, haha!!! looks kind of stupid...

#include <iostream> using namespace std; //#define BUILD_ON_VS class CC { public: template<typename T> struct Foo; Foo<int>* m_foo; template<typename T> struct Foo { template<T foo> void bar(); }; }; template<typename T> template<T foo> void CC::Foo<T>::bar() { cout << "VC happen to like this...\n"; } int main() { CC cc; cc.m_foo = new CC::Foo<int>; cc.m_foo->bar<2>(); }

I need to make an abstract class and instantiate it with template arguments;

Wonder why VC can't do this automatically like GCC.


  • Zend DB Select with multiple table joins
  • django - can't see the uploaded media files from apache
  • QML cannot assign assign shared_ptr to [unknown property type]
  • iOS, How to check whether the user likes a facebook page or not?
  • Member function pointer cast, from Derived to Base class
  • Exposing an enum in a QML signal
  • What's the essential difference between these two variadic functions?
  • std::system Exception when instantiating a singleton object
  • How to unwind to the first view controller on a navigation stack
  • What is the Linux Equivalent of Kernel32.dll?
  • not able to create VC++ project, with VS11
  • Partial specialization of a class template in derived class affects base class
  • incomplete type 'struct' error in C
  • Monotouch crashes with NullReferenceException on non nullable object
  • c++ regex_replace not doing intended substitution
  • sweetalert2 inputoptions from file in select example
  • VBA Excel, loop through variables
  • NHibernate manually control fetching
  • Simple linked list-C
  • Reduction and collapse clauses in OMP have some confusing points
  • Meteor helpers not available in Angular template
  • JFileChooser in front of fullscreen Swing application
  • HTML download movie download link
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Possible to stop flickering java tooltip in heavyweight mode?
  • Window Size for Mac application
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • Build own AppleScript numerical error handling
  • VB.net deserialize, JSON Conversion from type 'Dictionary(Of String,Object)' to type '
  • Suggestions to manage Login/Logout transitions
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?
  • How to load view controller without button in storyboard?