Thread: template functions as member functions

  1. #1
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    South Pasadena, CA
    Posts
    36

    template functions as member functions

    I want to have a template function that is a member of a class. Is this possible? This code snippet is how I would think the syntax would go, although it doesn't compile. How would I achieve the same effect?

    Code:
    class myclass
        {
        public:
    	int member ;
        } ;
    
    template <typename T> void myclass::func( T& arg )
        {
        T = member ;
        }
    
    int main()
        {
        myclass instance ;
        instance.member = 0 ;
    
        int copy ;
        instance.func<int>(copy) ;
    
        double copy2 ;
        instance.func<double>(copy2) ;
        }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    I think you might have to template your class as well. But what's wrong with compiling the code you wrote yourself?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I would expect:
    Code:
    class myclass
    {
    public:
        int member;
    
        template <typename T> void func(T& arg)
        {
            arg = member;
        }
    };
    After all, if you want a member function, then you need to declare it within the class definition, so if you want a member function template, then declaring it within the class definition makes sense. I have chosen to define it within the class definition too, but that is optional.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Optics with a Twist skewray's Avatar
    Join Date
    Dec 2006
    Location
    South Pasadena, CA
    Posts
    36
    Thanks, laserlight. Id did have a stupid type on there. What I originally couldn't get working was when the class is itself a template:

    Code:
    template <int N>
    class myclass
        {
        public:
            int member ;
            template <typename T> void func( T& arg ) ;
        } ;
    
    template <int N> template <typename T>
    void myclass<N>::func( T& arg )
        {
        arg = member ;
        }
    Works now. I was using template<int I,typename T>, which doesn't work.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Good to hear. By the way, instead of:
    Code:
    instance.func<int>(copy);
    You might as well write:
    Code:
    instance.func(copy);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I get a set of functions to all use the same template?
    By MutantJohn in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2013, 12:54 PM
  2. template functions?
    By ovid in forum C++ Programming
    Replies: 8
    Last Post: 02-25-2010, 09:15 AM
  3. using Template Functions
    By ashcan1979 in forum C++ Programming
    Replies: 3
    Last Post: 09-20-2006, 12:44 AM
  4. Class member variables only readable by member functions?
    By _Elixia_ in forum C++ Programming
    Replies: 4
    Last Post: 10-10-2003, 03:52 PM
  5. Borland, libraries, and template member functions
    By roktsyntst in forum C++ Programming
    Replies: 0
    Last Post: 06-01-2003, 09:52 PM

Tags for this Thread