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

  • Context: C/C++ 
  • Thread starter Thread starter Carno Raar
  • Start date Start date
  • Tags Tags
    c++
Click For Summary

Discussion Overview

The discussion revolves around the practice of forward declaring templates in C++. Participants explore the implications, potential issues, and stylistic considerations associated with this practice, referencing various sources and guidelines.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant questions why leading C++ experts consider forward declaring templates to be bad style or dangerous.
  • Another participant suggests an article that discusses declaration versus definition but finds it does not address the concerns about forward declaring templates.
  • A participant cites Google's C++ style guide, stating that forward declaring a template can lead to code duplication and hinder compatibility changes.
  • One participant challenges the reasoning provided by Google's style guide, proposing that authors can create separate headers for forward declarations and referencing the C++ standard library's practices.
  • A later reply emphasizes that forward declaring templates can lead to undefined behavior, highlighting the risks of relying on specific compiler implementations.
  • Another participant introduces the PIMPL idiom as a potential solution to the issues associated with forward declaring templates, explaining its structure and benefits.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness and implications of forward declaring templates, with no consensus reached on the best practices or reasoning behind the concerns.

Contextual Notes

Some participants reference specific guidelines and practices from the C++ standard library, but the discussion remains open-ended regarding the broader implications and best practices for forward declaring templates.

Carno Raar
Messages
90
Reaction score
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
.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.
 
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.
 
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).
 

Similar threads

  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
Replies
5
Views
8K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K