43092

Initializing a static const variable gives me template definition of non-template const

Here is my code :

#include <iostream> class c{ public: int y; c(int x,int p):y(x){ std::cout<<"has been made"<<std::endl; } }; template<int x,typename B = void> struct checkeven{ }; template<typename B> struct checkeven<0,B>{ typedef B type; }; template<int x,int y,class = typename checkeven<x%2,int>::type> struct t{ static const c tee; static const inline c& initor(){ return tee; } }; template<int x,int y> const c t<x,y>::tee(x,y); //how do i initialize? int main(int argc, char** argv) { //t<2,1>::initor(); //t<2,2>::initor(); return 0; }

I tried researching it about but i can't find anything except the suggestion to remove the default value duplicate. I want to implement SFINAE to make sure that the first value is an even number. I have a static const variable that i want to initialize. It was working fine without my default class but once i added it, things got messy. How do i initialize my const static member? I would also like to know why my code isn't working.

Answer1:

You need to specialize your template struct t like that :

template<int x, int y> struct t<x,y, typename checkeven<x%2,int>::type> { static const c tee; static const inline c& initor(){ return tee; } };

Using this you'll be able to initialize your variable tee like that :

template<int x, int y> const c t<x, y>::tee(x, y);

Live example here

Recommend

  • Why does tee wait for all subshells to finish?
  • Using cat and execvp
  • what's the best way to checkout from TFS to eclipse using TEE?
  • Redirect stdout of one process to two processes
  • Bundled scripts not working MVC
  • “class template has already been declared as a non-class template”
  • css Star-rating html
  • Member function pointer cast, from Derived to Base class
  • Multiple sockets for clients to connect to
  • Complex trait requirements on struct
  • iOS - Is this a task for enums?
  • How to align an image side by side with a heading element?
  • Partial specialization of a class template in derived class affects base class
  • Is there a parser equivalent of 'fragment' marking in ANTLR4?
  • Monotouch crashes with NullReferenceException on non nullable object
  • sweetalert2 inputoptions from file in select example
  • C++ Single function pointer for all template instances
  • VBA Excel, loop through variables
  • Simple linked list-C
  • how to adjust image in a panel in Java swing?
  • Spring Data JPA custom method causing PropertyReferenceException
  • req.body is undefined - nodejs
  • sending/ receiving email in Java
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Circular dependency while pushing http interceptor
  • Unanticipated behavior
  • Linker errors when using intrinsic function via function pointer
  • How to delete a row from a dynamic generate table using jquery?
  • Proper way to use connect-multiparty with express.js?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • EntityFramework adding new object to nested object collection
  • Checking variable from a different class in C#
  • Binding checkboxes to object values in AngularJs
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?