Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

forward declaration of using in c++11

I'm trying to use a type alias of an object in another header without including header file .

My simplified version of code is :

// A.h
    #include <vector>
    using Vector=std::vector<int>;

====================================================

//B.h
using Vector;//forward declaration but not working !(Vector has not beed declared)
int foo(Vector*);

====================================================
//B.cpp
#include "A.h"
void foo(Vector*){}

I don't want to write using Vector=std::vector<int>; again in B.h because It's definition must be same as definition of Vector in A.h and It may change in future and I can't include it because my code have circular dependency.

Is forward declaration of using possible in c++11 ?

like image 529
uchar Avatar asked Jun 12 '14 13:06

uchar


People also ask

What are forward declarations in C?

In C++, Forward declarations are usually used for Classes. In this, the class is pre-defined before its use so that it can be called and used by other classes that are defined before this. Example: // Forward Declaration class A class A; // Definition of class A class A{ // Body };

How do you use a forward declaration?

To write a forward declaration for a function, we use a function declaration statement (also called a function prototype). The function declaration consists of the function header (the function's return type, name, and parameter types), terminated with a semicolon. The function body is not included in the declaration.

What is forward declaration in Objective C?

In Objective-C, classes and protocols can be forward-declared like this: @class MyClass; @protocol MyProtocol; In Objective-C, classes and protocols can be forward-declared if you only need to use them as part of an object pointer type, e.g. MyClass * or id<MyProtocol>.

Where do you put forward declaration?

Generally you would include forward declarations in a header file and then include that header file in the same way that iostream is included.


1 Answers

It is not possible, however the following could be a workaround.

// Common.h
...
#include <vector>
using Vector=std::vector<int>;
...

Afterwards you include Common.h in both, A.h and B.h.

like image 113
Theolodis Avatar answered Oct 03 '22 12:10

Theolodis