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

  • Thread starter Thread starter Carno Raar
  • Start date Start date
  • Tags Tags
    c++
Click For Summary
SUMMARY

Forward declaring templates in C++ is discouraged due to the potential for code duplication and undefined behavior across different compilers. Google's C++ style guide explicitly states that such practices can hinder compatibility and maintainability. The standard library prohibits forward declarations of templates, emphasizing that it can lead to undefined behavior. Instead, developers are encouraged to use the PIMPL idiom (Pointer to Implementation) to encapsulate implementation details while maintaining a clean interface.

PREREQUISITES
  • Understanding of C++ templates and their usage
  • Familiarity with the C++ standard library
  • Knowledge of the PIMPL idiom in C++
  • Awareness of compiler behavior and compatibility issues
NEXT STEPS
  • Read Google's C++ Style Guide for best practices on template usage
  • Study the PIMPL idiom for effective class design in C++
  • Explore the implications of undefined behavior in C++ programming
  • Investigate the differences in template handling across various C++ compilers
USEFUL FOR

C++ developers, software engineers, and anyone involved in template programming and code optimization in C++. This discussion is particularly beneficial for those looking to improve code maintainability and compatibility across different environments.

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).
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

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