标签: forward-declaration

重构C++代码以使用前向声明

我有一个很长的代码库已经存在了一段时间,我试图通过重构来整理它.我想做的一件事是找到我可以转发声明成员的所有标题,而不是包括整个头文件.

这是一个相当费力的过程,我正在寻找一个工具,可以帮助我找出哪些标题包含可以向前声明的成员.

是否有编译器设置可能会发出警告或建议以下代码可以使用前向声明?我正在使用以下编译器icc,gcc,sun studio和HP的aCC

是否有可以完成相同工作的独立工具?

#include "Foo.h"
...//more includes

class Bar {
.......
private:
    Foo* m_foo;
};
Run Code Online (Sandbox Code Playgroud)

c++ compiler-construction refactoring forward-declaration

19
推荐指数
2
解决办法
1030
查看次数

"使用命名空间"到底做了什么?

以下C++测试代码没有链接(gcc 4.9.2,binutils 2.25).错误是In function 'main': undefined reference to 'X::test'.

01: #include <string>
02: #include <iostream>
03:
04: namespace X
05: {
06:     extern std::string test;
07: };
08:
09: using namespace X;
10: std::string test = "Test";
11:
12: int main()
13: {
14:    std::cout << X::test << std::endl;
15: }
Run Code Online (Sandbox Code Playgroud)

由于第09行,我期待第10行定义X::test在第06行声明的变量.我相信test在全局命名空间中声明和定义了一个不相关的变量,因此链接错误.

问题:有人可以解释为什么我的期望不正确,究竟发生了什么?

不是答案:

  • 我可以将它改为第10行std::string X::test = "Test";.
  • 我不应该使用"using namespace"开头.

c++ scope namespaces using-directives forward-declaration

19
推荐指数
2
解决办法
3413
查看次数

带前向声明的默认模板参数

是否可以转发声明一个使用默认参数的类而不指定或知道这些参数?

例如,我想boost::ptr_list< TYPE >在Traits类中声明一个,而不将整个Boost库拖动到包含特征的每个文件中.我想声明 namespace boost { template<class T> class ptr_list< T >; },但这不起作用,因为它与真正的类声明不完全匹配:

template < class T,
    class CloneAllocator = heap_clone_allocator,
    class Allocator = std::allocator<void*>
    >
class ptr_list { ... };
Run Code Online (Sandbox Code Playgroud)

我的选择只是与它一起生活或boost::ptr_list< TYPE, boost::heap_clone_allocator, std::allocator<void*>在我的特质课程中指定吗?(如果我使用后者,我想也必须转发声明boost::heap_clone_allocator和包含<memory>,我想.)

我查看了Stroustrup的书,SO和其他互联网,但没有找到解决方案.通常人们担心不包括STL,解决方案是"只包括STL标题".但是,Boost是一个更大规模和编译器密集型的库,因此除非我绝对必须,否则我更愿意将其删除.

c++ templates forward-declaration

18
推荐指数
3
解决办法
9246
查看次数

内联函数的前向声明

我有一个头文件,将包含大量(30+)内联函数.

我没有让读者滚动或搜索内联函数的定义(实现),而是希望有一个前向声明部分,它声明函数声明以及描述函数的注释.本节将允许读者了解如何使用函数或查找函数,而无需向下滚动到实现.

此外,我希望读者养成使用函数的习惯,而不必看到他们的实现.

独立函数的前向声明的语法是什么?

{这适用于C99和C++}

仅供参考,我使用IAR Workbench C编译器设置使用C99.

c c++ inline c99 forward-declaration

18
推荐指数
1
解决办法
8509
查看次数

为什么这是C++中的前向声明?

我将在utilA.cpp中有以下代码片段:

// utilB.h
namespace xm
{
     void zoo(struct tm timeval);  //<-----line 0
}


// utilA.cpp
#include <utilB.h>                 //<----line 1
#include <time.h>                  //<----line 2
namespace xm
{
     void foo()
     {
         struct tm time1 = {0};    //<----line 3
     }
}
Run Code Online (Sandbox Code Playgroud)

GCC在编译utilA.cpp时抱怨,

error: variable 'xm::tm time1' has initializer but incomplete type
Run Code Online (Sandbox Code Playgroud)

这似乎是因为它在第0行utilA.h使用struct tm,但没有包括time.h,并且编译器将第struct tm0行视为前向声明,因此第struct tm2 xm::tm行在第0行的头部内被解析.

那么C++标准是否将此struct tm函数参数定义为前向声明?请帮助解释一下,标准中的引用会有所帮助.

c++ standards forward-declaration language-lawyer

18
推荐指数
2
解决办法
1461
查看次数

Django模型:两个类之间的相互引用和在python中使用前向声明的不可能性

我定义了两个模型,每个模型引用另一个模型,如下所示:

class User(models.Model):
    # ...
    loves = models.ManyToManyField(Article, related_name='loved_by')

class Article(models.Model):
    # ...
    author = models.ForeignKey(User)
Run Code Online (Sandbox Code Playgroud)

你看,问题是两个类都互相引用.无论这两个类的实现顺序如何,python总是引发NameError异常,抱怨任何一个类都没有定义.

python django model foreign-keys forward-declaration

17
推荐指数
1
解决办法
4242
查看次数

课堂上的enum前言可能吗?

我知道在C++ 11中可以转发声明枚举类型(如果提供了存储类型),例如

enum E : short;
void foo(E e);

....

enum E : short
{
    VALUE_1,
    VALUE_2,
    ....
}
Run Code Online (Sandbox Code Playgroud)

但我想转发声明在类中定义的枚举,例如

enum Foo::E : short;
void foo(E e);

....

class Foo
{
    enum E : short
    {
        VALUE_1,
        VALUE_2,
    ....
    }
}
Run Code Online (Sandbox Code Playgroud)

在C++ 11中是否可以这样?

c++ enums nested forward-declaration c++11

17
推荐指数
2
解决办法
9617
查看次数

不能使用std :: unique_ptr <T>,T是前向声明

首先,我知道unique_ptr <>和转发声明的一般问题,如使用unique_ptr的转发声明?.

考虑这三个文件:

#include <memory>
#include <vector>

class B;

class A
{
public:
    ~A();

private:
    std::unique_ptr<B> m_tilesets;
};
Run Code Online (Sandbox Code Playgroud)

C.cpp

#include "A.h"

class B {

};

A::~A() {

}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <memory>

#include "A.h"

int main() {
    std::unique_ptr<A> m_result(new A());
}
Run Code Online (Sandbox Code Playgroud)

发出会g++ -std=c++11 main.cpp C.cpp产生以下错误:

In file included from /usr/include/c++/4.8/memory:81:0,
                 from main.cpp:1:
/usr/include/c++/4.8/bits/unique_ptr.h: In instantiation of ‘void std::default_delete<_Tp>::operator()(_Tp*) const [with _Tp = B]’:
/usr/include/c++/4.8/bits/unique_ptr.h:184:16:   required from ‘std::unique_ptr<_Tp, _Dp>::~unique_ptr() [with _Tp = B; _Dp = std::default_delete<B>]’
A.h:6:7: …
Run Code Online (Sandbox Code Playgroud)

c++ forward-declaration unique-ptr c++11

17
推荐指数
2
解决办法
6533
查看次数

`def` vs`declare`用于前向声明

Clojure,有一个declare宏,允许您转发声明函数或变量.这似乎完全一样的功能def:无论(declare x)(def x)创建#<Unbound Unbound: #'user/x>

何时应该(declare x)使用而不是(def x)

clojure forward-declaration

17
推荐指数
2
解决办法
668
查看次数

可变参数模板仅在向前声明时编译

I have a variadic template that inherits from all template arguments:

template <typename... Ts>
struct derived : Ts...
{
};
Run Code Online (Sandbox Code Playgroud)

I would also like to have a facility for expressing the type of "existing derived with added template arguments". My attempt at this is:

// Do not ODR-use (goes in namespace impl or similar)!
template<class ... NewInputs, class ... ExistingInputs>
auto addedHelper(const derived<ExistingInputs...>&)
    -> derived<ExistingInputs..., NewInputs...>;

template<class ExistingInput, class ... NewInputs>
using Added = decltype(addedHelper<NewInputs...>(std::declval<ExistingInput>()));
Run Code Online (Sandbox Code Playgroud)

As a simple example, Added<derived<A, …

c++ forward-declaration language-lawyer variadic-templates c++17

17
推荐指数
1
解决办法
308
查看次数