Thread: How do I separate function definitions from a template class to .cpp file?

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    217

    How do I separate function definitions from a template class to .cpp file?

    I have a templated class. I want to put the definitions of its functions in a .cpp file, how do I do this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why can’t I separate the definition of my templates class from its declaration and put it inside a .cpp file?

    That said, you can separate it, e.g., into a .ipp file (or just .cpp, but the Boost .ipp convention is useful to avoid confusion), then include the implementation file in translation units where you need the template to be instantiated rather than just the forward declaration in the header.
    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. Replies: 10
    Last Post: 11-13-2015, 03:15 PM
  2. Help with using a Function from a separate file.
    By RelentlessWiz in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2011, 03:33 PM
  3. Using Template Class In Separate Class
    By SterlingM in forum C++ Programming
    Replies: 2
    Last Post: 03-03-2011, 05:34 PM
  4. Replies: 3
    Last Post: 01-30-2011, 04:28 PM
  5. Function template in Class template?
    By Aidman in forum C++ Programming
    Replies: 3
    Last Post: 10-28-2003, 09:50 AM

Tags for this Thread