我花了一些时间来检查boost::库架构,并对以下事实感兴趣:
在库的某些部分中,使用的yyy_fwd.hpp想法很常见(参见boost/detail或boost/flyweight示例).
这些文件显然只包含一些基于模板的类的前向声明,据我所知,可以在编译时间方面受益.
有人可以指出它们在什么情况下有所帮助,我应该在设计自己的模板时使用相同的想法吗?
谢谢.
考虑这个代码,有一个明显的编译错误:(1)
struct A;
struct B {
B() { new A(); } // error: allocation of incomplete type 'A'
};
Run Code Online (Sandbox Code Playgroud)
使用a unique_ptr也无济于事:(2)
struct A;
struct B {
B() { std::make_unique<A>(); } // error: due to ~unique_ptr()
};
Run Code Online (Sandbox Code Playgroud)
然后(令我惊讶的是)我发现,这将编译:(3)
struct A;
struct B {
B() { std::make_unique<A>(); }
};
struct A {}; // OK, when a definition is added **below**
Run Code Online (Sandbox Code Playgroud)
然后我检查了,这是否有帮助new- 不:(4)
struct A;
struct B {
B() { new A(); } // error: allocation of …Run Code Online (Sandbox Code Playgroud) 我现在通过Zed A. Shaw的"学习C艰难之路"学习C编程.有这个代码(取自他的网站):
#include <stdio.h>
#include <ctype.h>
// forward declarations
int can_print_it(char ch);
void print_letters(char arg[]);
void print_arguments(int argc, char *argv[])
{
int i = 0;
for(i = 0; i < argc; i++) {
print_letters(argv[i]);
}
}
void print_letters(char arg[])
{
int i = 0;
for(i = 0; arg[i] != '\0'; i++) {
char ch = arg[i];
if(can_print_it(ch)) {
printf("'%c' == %d ", ch, ch);
}
}
printf("\n");
}
int can_print_it(char ch)
{
return isalpha(ch) || isblank(ch);
}
int main(int …Run Code Online (Sandbox Code Playgroud) 我正在尝试通过使用前向声明并将#includes移动到实现文件中来简化一堆头文件"include spaghetti".但是,我继续遇到以下情况:
//Foo.h
#include "Bar.h"
class Foo
{
public:
void someMethod(Bar::someType_t &val);
};
//Bar.h
.
.
.
class Bar
{
public:
typedef std::vector<SomeClass> someType_t;
};
Run Code Online (Sandbox Code Playgroud)
我希望在尽可能多的情况下删除#include"Bar.h".我还看到Bar.h中的typedef列在Bar类之外的情况.我假设两种情况都可以用同样的方式解决.
有任何想法吗?
我正在使用一个C++第三方库,它将所有类放在一个版本化的命名空间中,让我们调用它tplib_v44.它们还定义了通用名称空间别名:
namespace tplib = tplib_v44;
Run Code Online (Sandbox Code Playgroud)
如果使用通用命名空间在我自己的.h文件中正向声明库的成员...
namespace tplib { class SomeClassInTpLib; }
Run Code Online (Sandbox Code Playgroud)
...我在第三方库的头文件中遇到编译器错误(稍后将在我的.cpp实现文件中包含):
error C2386: 'tplib' : a symbol with this name already exists in the current scope
Run Code Online (Sandbox Code Playgroud)
如果我使用特定于版本的命名空间,那么一切正常,但那么......重点是什么?处理这个问题的最佳方法是什么?
[编辑]仅供未来观众使用,这是ICU图书馆.解决方案(至少在我的情况下)是对已接受答案的评论.
任何人都可以在目标C中为正常的类而不是类别或协议提供前向声明的示例吗?
class objective-c forward-declaration objc-protocol objc-category
传递声明的struct或类时,必须通过引用或指针将其传递给函数.
但是,使用前向声明的枚举可以做些什么?它是否也必须通过引用或指针传递?或者,它可以传递一个值吗?
下一个例子使用g ++ 4.6.1编译好:
#include <iostream>
enum class E;
void foo( const E e );
enum class E
{
V1,
V2
};
void foo( const E e )
{
switch ( e )
{
case E::V1 :
std::cout << "V1"<<std::endl;
break;
case E::V2 :
std::cout << "V2"<<std::endl;
break;
default:
;
}
}
int main()
{
foo( E::V1);
foo( E::V2);
}
Run Code Online (Sandbox Code Playgroud)
建立:
g++ gy.cpp -Wall -Wextra -pedantic -std=c++0x -O3
Run Code Online (Sandbox Code Playgroud)
以上标准是否合规,或者是否使用扩展名?
我在Delphi XE8中有一个类的以下声明:
TestClass = class;
TestClass = class
function test<T: TestClass>(supplier: TFunc<T>): T; // Compiler error
end;
Run Code Online (Sandbox Code Playgroud)
这会抛出以下编译器错误:
E2086 Type 'TestClass' is not yet completely defined
Run Code Online (Sandbox Code Playgroud)
当我在混合中添加另一个类并将其作为约束使用时,它可以正常工作:
AnotherTestClass = class
end;
TestClass = class;
TestClass = class
function test<T: AnotherTestClass>(supplier: TFunc<T>): T; // No Error
end;
Run Code Online (Sandbox Code Playgroud)
我怀疑问题是前向类型声明没有告诉Delphi足够的TestClass类型.这可能更明显,因为以下尝试解决该问题会在不同的行上抛出相同的编译器错误:
TestClass = class;
AnotherTestClass = class (TestClass) // Compiler Error
end;
TestClass = class
function test<T: AnotherTestClass>(supplier: TFunc<T>): T;
end;
Run Code Online (Sandbox Code Playgroud)
我做错了什么,如果没有,是否有解决这个问题的方法?
delphi generics forward-declaration type-constraints delphi-xe8
我知道,一般来说,我们可以在 C++11 中前向声明枚举。
那么,为什么会这样:
enum kind_t { kind1, kind2 };
template <kind_t Kind> struct foo {};
template <> struct foo<kind1> {
enum named : int;
};
enum foo<kind1>::named : int {
named1 = 123,
named2 = 456,
};
Run Code Online (Sandbox Code Playgroud)
使用 GCC (12.1) 编译失败?错误是(上帝螺栓):
<source>:9:6: error: cannot add an enumerator list to a template instantiation
9 | enum foo<kind1>::named : int {
| ^~~~~~~~~~
ASM generation compiler returned: 1
<source>:9:6: error: cannot add an enumerator list to a …Run Code Online (Sandbox Code Playgroud) 我知道这些问题似乎含糊不清,但我想不出任何其他方式来表达它,但是,是否有可能做到这样的事情:
#include<iostream>
class wsx;
class wsx
{
public:
wsx();
}
wsx::wsx()
{
std::cout<<"WSX";
}
Run Code Online (Sandbox Code Playgroud)
?
c++ ×7
c++11 ×2
class ×2
enums ×2
boost ×1
c ×1
declaration ×1
definition ×1
delphi ×1
delphi-xe8 ×1
gcc ×1
generics ×1
namespaces ×1
objective-c ×1
templates ×1
typedef ×1