[C++] When is it OK to forward declare templates?

  • C/C++
  • Thread starter Carno Raar
  • Start date
  • Tags
    c++
In summary: The handle is simply a pointer to that class. This separates the interface from the implementation, so you can change the implementation without changing the interface.
  • #1
Carno Raar
90
19
This has been bugging me for a while. I suspect the answer is in Sutter or Meyers but I don't own them. Why do so many of the top C++ people say forward declaring templates is bad style or dangerous?
 
Technology news on Phys.org
  • #3
.Scott said:

Either I am misunderstanding, or this doesn't relate to the question?

As far as I can tell it explains the difference between declaration and definition and makes no comment as to why forward declaring a template is frowned on.
 
  • #4
I have since found the answer by using Google which returned Google's C++ style guide which explains why they don't allow it: Forward declaring a template creates code duplication which prevents the header's owner from making compatible changes to the template.
 
  • #5
Carno Raar said:
I have since found the answer by using Google which returned Google's C++ style guide which explains why they don't allow it: Forward declaring a template creates code duplication which prevents the header's owner from making compatible changes to the template.
That's a lousy excuse. There is a way around that excuse: The author of the template writes another header where the template is forward declared. Note that the C++ library does this, but only with the C++ I/O. If you have a list as a data member of some class, you need the full implementation anyhow.

There is a good reason for not forward declaring templates in your code defined in the standard library. It's illegal, and it's the worst kind of illegal. It's undefined behavior. The standard makes this very explicit. It might work on your computer with your current version of your compiler. Go to a different machine, or a different compiler, and it might not work. Heaven forbid, it might even create nasal demons.

There is a way around the difficulties of forward declaring templates, and that's the PIMPL idiom (aka the handle/body implementation). You hide all the details of the implementation (including member data) in a separate class. The public class is very simple. It comprises public member functions and a single private data member. That private data member is a pointer to that separate class where all the work is done, all the data are stored (except for the handle, of course).
 

Related to [C++] When is it OK to forward declare templates?

1. When should I use forward declaration for templates?

Forward declaration for templates should be used when you want to declare a template class or function without defining its implementation. This can be useful in cases where the template is not fully utilized or when you want to reduce compile time by avoiding unnecessary header file inclusion.

2. Can I forward declare all templates?

No, not all templates can be forward declared. Only templates with non-dependent types can be forward declared. Templates with dependent types, such as those that use the typename keyword, cannot be forward declared.

3. How do I forward declare a template class?

To forward declare a template class, you need to specify the template parameters in angle brackets after the class name. For example, template <typename T> class MyClass; This will declare a template class named MyClass with a single template parameter T.

4. What are the benefits of using forward declaration for templates?

Forward declaration for templates can reduce compile time by avoiding unnecessary header file inclusion. It can also help to reduce code dependencies and improve modularity by separating the declaration and implementation of templates.

5. Are there any drawbacks to using forward declaration for templates?

One drawback of using forward declaration for templates is that it can make the code more complex and difficult to understand. It may also require additional effort to maintain and update the code in the future. Additionally, forward declaration may not be suitable for all situations and may not always result in a significant improvement in compile time.

Similar threads

  • Programming and Computer Science
Replies
23
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
2K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
8
Views
967
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
21
Views
3K
  • Programming and Computer Science
Replies
5
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
21
Views
1K
Back
Top