Forward declarations in C ++ - when does it matter?

I think this is the spirit of C ++ - you do not pay for what you do not want (you obviously pay for what you need):

// a.h
#include <iosfwd>

template< class T >
class QVector;

struct A
{
void process( QVector<int> );

void print( std::ostream& );
};

// some.cpp

#include "a.h"

#include <iostream> // I need only A::print() in this module, not full interface

...
A().print( std::cout );
...

That's why I think it’s unfair to prevent a developer from working this way with STL ( Will there be declaration files in C ++ 11 STL? ).

But also I see one bad thing : the dependencies of module A will spread in the external context (duplication of directives #include), and this can lead to hard refactoring when the interface changes (for example, replace QVector with QList - and now you need to replace all the entries <QVector>with <QList>).

Solution to this problem:

#include <iostream>
#include <QVector>

struct A
{
void process( QVector<int> );

void print( std::ostream& );
};

" " - ( )? , (, Qt ).

- ( ):

// a_decl.h
#include <iosfwd>

template< class T >
class QVector;

struct A
{
void process( QVector<int> );

void print( std::ostream& );
};

// a.h
// Include this file if you want to use most interface methods
// and don't want to write a lot of `#include`
#include <iostream>
#include <QVector>

#include "a_decl.h"

, .

? ? ?

( )

UPDATE:

boost 1.48.0 Container, undefined ( ).

+1
2

++ - , , , .

IMO, , "", , .h, - , " ", .

"" "" , : " ++" Lakos, .

, Lakos "in-name-only" "in-size"; ( ) :

. f T , f T.

. f T , f , f, T.

()

, ++ , , , .

, .

+1

.

.

/ / .

, , .

, . " #include <string> , ( ).

, , , .. , - , .

+1

All Articles