Thread: static variable in templates

  1. #1
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking static variable in templates

    I ran into a problem with templates. What do I do to access a static member for a templated class?
    I want the same static variable for every type of instantaniation of the class.
    When I tried these I got errors.
    Code:
    template<int ivar = 5>
    class MyClass{
    public:
    static double stvar;
    //blah...
    };
    
    MyClass::stvar = .5;        //syntax error before '::' token.
    double MyClass::stvar = .5;   //Multiple defenition of MyClass<5>::stvar
    What is happening here? and How do I access the static variable?

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    I don't know what you're trying to do here, but if you're trying to initialize the variables, then you do it in the constructor.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Try this:
    Code:
    #include<iostream>
    using namespace std;
    
    template<int ivar = 5>
    class MyClass
    {
    public:
    	static double stvar;
    	
    };
    
    template<int ivar> 
    double MyClass<ivar>::stvar = .5;
    
    
    int main()
    {
    	cout<<MyClass<30>.stvar<<endl;
    	cout<<MyClass<60>.stvar<<endl;
    
    	return 0;
    }
    I don't know too much about templates, but from what I can gather it's generally not a good idea to use non-type parameters because they are restrictive, and templating a class with only a non-type parameter doesn't seem like a very good idea.

    I think you can accomplish what you want doing this:
    Code:
    #include<iostream>
    using namespace std;
    
    template<typename T>
    class MyClass
    {
    public:
    	static double stvar;
    	
    	//some stuff with type T
    	
    };
    
    template<typename T> 
    double MyClass<T>::stvar = .5;
    
    
    int main()
    {
    	cout<<MyClass<int>.stvar<<endl;
    	cout<<MyClass<double>.stvar<<endl;
    	
    
    	return 0;
    }
    Last edited by 7stud; 11-20-2005 at 02:21 PM.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if you're trying to initialize the variables, then you do it in the constructor
    Static variables are not initialized in the constructor, arjunjay had it right but did not specify the template argument.

    >> from what I can gather it's generally not a good idea to use non-type parameters because they are restrictive
    There's nothing wrong with doing that. Look at boost::array.

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Quote Originally Posted by Daved
    >> from what I can gather it's generally not a good idea to use non-type parameters because they are restrictive
    There's nothing wrong with doing that. Look at boost::array.
    From what I've read, the primary rationale for non-type parameters is precisely for specifying size and range limits for containers. Is that what the op is doing?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's irrelevant to the question, and there is no way of knowing anyway, since the posted code doesn't show how ivar is used one way or the other. So you should be careful about making a blanket statement regarding something not being a good idea when it is perfectably acceptable in many instances, one of which might be currently in use.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    So you should be careful about making a blanket statement regarding something not being a good idea
    I didn't make a blanket statement, I qualified it, and I gave the reason why. In addition, I gave an example of how the same thing could be achieved without a non-type parameter.
    Last edited by 7stud; 11-20-2005 at 05:15 PM.

  8. #8
    ... arjunajay's Avatar
    Join Date
    May 2005
    Posts
    203

    Talking Thanks

    Thanks for this.
    template<int ivar>
    double MyClass<ivar>::stvar = .5;
    >>From what I've read, the primary rationale for non-type parameters is precisely for specifying >>size and range limits for containers. Is that what the op is doing?
    Yup.

    >>In addition, I gave an example of how the same thing could be achieved without a non-type >>parameter.
    In your example how does the 'typename' stand instead of the array size which is a integer and not a type?

    From your examples I also come to understand that you have different static variables for different parameters which is something I want to avoid.Can it be done?
    I'm not using Global variables because I find most posters say its really BAAAAD.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The only way to get one common static variable in all instantiations of a template is to put it into a base class. Since that base is an implementation detail, it should be inherited as private or protected:
    Code:
    class MyBase
    {
    protected:
      static int instanceCount;
    };
    
    template<typename T>
    class MyTemplate : private MyBase
    {
      void foo() {
        trace(instanceCount);
      }
    };
    };
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scope of global variable vs. static
    By chiefmonkey in forum C++ Programming
    Replies: 4
    Last Post: 06-21-2009, 12:23 PM
  2. Replies: 20
    Last Post: 01-26-2009, 01:33 AM
  3. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  4. what is static variable....?
    By gardenair in forum C Programming
    Replies: 4
    Last Post: 04-09-2003, 08:54 AM
  5. Replies: 3
    Last Post: 10-10-2002, 07:34 AM