使用模板时将定义与声明分开 [英] separating definitions from declarations when using templates

查看:73
本文介绍了使用模板时将定义与声明分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我一直在写一些代码,到目前为止,我已经将所有代码写在

.h文件中避免链接器错误。我正在使用模板。

我想将实现移动到.cpp文件。在读完一些

之后,我发现唯一的方法是在.cpp文件中添加实际的

模板实例。但是,如果

你没有使用内置类型,你怎么做?例如,模板类

可能是,


模板

<

类目标,

class StoragePolicy,

int k

Hi everyone,

I''ve been writing some code and so far I have all the code written in
the .h files in order to avoid the linker errors. I''m using templates.
I wanted to move the implementations to the .cpp files. After some
reading, I found that the only way to do this is to add the actual
template instantiations in the .cpp file. But, how do you do that if
you''re not working with built-in types? For example, a template class
might be,

template
<
class Objective,
class StoragePolicy,
int k


>
>



class ClassA {

....

//声明和定义

....

}


如果我的目标和
$ b $,如何将其分成.cpp和.h文件b StoragePolicy是由代码的用户定义的?在这个例子中,

StoragePolicy是我可以定义的(让我们说

std :: vector),但Objective是用户提供的对象。

有办法解决这个问题吗?我还读了一些关于使用

" export"关键字,但它似乎不适用于gnu g ++。任何帮助

将不胜感激。


a ^ 2

class ClassA {
....
// declarations and definitions
....
}

How do I separate this into .cpp and .h files if I Objective and
StoragePolicy are defined by the user of the code? In this example,
StoragePolicy is something that I could define (let''s say
std::vector), but Objective is an object that the user provides. Is
there a way to get around this? I read also something about using the
"export" keyword but it doesn''t seem to work with gnu g++. Any help
will be appreciated.

a^2

推荐答案

aaragon写道:
aaragon wrote:

大家好,


我一直在写一些代码,到目前为止我有所有代码都写在

.h文件中,以避免链接器错误。我正在使用模板。

我想将实现移动到.cpp文件。在读完一些

之后,我发现唯一的方法是在.cpp文件中添加实际的

模板实例。但是,如果

你没有使用内置类型,你怎么做?例如,模板类

可能是,


模板

<

类目标,

class StoragePolicy,

int k
Hi everyone,

I''ve been writing some code and so far I have all the code written in
the .h files in order to avoid the linker errors. I''m using templates.
I wanted to move the implementations to the .cpp files. After some
reading, I found that the only way to do this is to add the actual
template instantiations in the .cpp file. But, how do you do that if
you''re not working with built-in types? For example, a template class
might be,

template
<
class Objective,
class StoragePolicy,
int k

>>
>>



class ClassA {

...

//声明和定义

...

}


如果我的目标和

StoragePolicy,如何将其分成.cpp和.h文件是由代码的用户定义的?

class ClassA {
...
// declarations and definitions
...
}

How do I separate this into .cpp and .h files if I Objective and
StoragePolicy are defined by the user of the code?



简答:你不是。

Short answer: you don''t.


在这个例子中,

StoragePolicy是我可以定义的(让我们说

std :: vector),但Objective是用户提供的对象。

有办法解决这个问题吗?我还读了一些关于使用

" export"关键字,但它似乎不适用于gnu g ++。
In this example,
StoragePolicy is something that I could define (let''s say
std::vector), but Objective is an object that the user provides. Is
there a way to get around this? I read also something about using the
"export" keyword but it doesn''t seem to work with gnu g++.



export关键字用于此目的,但是,大多数编译器都不支持
支持它。

The export keyword is meant for this purpose, however, most compilers don''t
support it.


任何帮助将不胜感激。
Any help will be appreciated.



您的选项包括:


a)将实现代码留在头文件中。这很常见。

练习。


b)使用支持export的编译器。这种情况并不常见,会让您的代码变得不那么便携。

最好


Kai-Uwe Bux

Your options include:

a) leave the implementation code in the header files. This is common
practice.

b) use a compiler that supports "export". This is not common and will render
you code less portable.
Best

Kai-Uwe Bux


2月9日下午6:08,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:
On Feb 9, 6:08 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

aaragon写道:
aaragon wrote:

大家好,
Hi everyone,


我一直在写一些代码到目前为止,我已将所有代码写在

.h文件中,以避免链接器错误。我正在使用模板。

我想将实现移动到.cpp文件。在读完一些

之后,我发现唯一的方法是在.cpp文件中添加实际的

模板实例。但是,如果

你没有使用内置类型,你怎么做?例如,模板类

可能是,
I''ve been writing some code and so far I have all the code written in
the .h files in order to avoid the linker errors. I''m using templates.
I wanted to move the implementations to the .cpp files. After some
reading, I found that the only way to do this is to add the actual
template instantiations in the .cpp file. But, how do you do that if
you''re not working with built-in types? For example, a template class
might be,


模板

<

类目标,

类StoragePolicy,

int k
template
<
class Objective,
class StoragePolicy,
int k


class ClassA {

...

//声明和定义

...

}
class ClassA {
...
// declarations and definitions
...
}


如果我的目标和

StoragePolicy由代码用户定义,如何将其分成.cpp和.h文件?
How do I separate this into .cpp and .h files if I Objective and
StoragePolicy are defined by the user of the code?



简答:你不是。


Short answer: you don''t.


在这个例子中,

StoragePolicy是我可以定义的(让我们说

std :: vector),但Objective是用户提供的对象。

有办法解决这个问题吗?我还读了一些关于使用

" export"关键字,但它似乎不适用于gnu g ++。
In this example,
StoragePolicy is something that I could define (let''s say
std::vector), but Objective is an object that the user provides. Is
there a way to get around this? I read also something about using the
"export" keyword but it doesn''t seem to work with gnu g++.



export关键字用于此目的,但是,大多数编译器都不支持
支持它。


The export keyword is meant for this purpose, however, most compilers don''t
support it.


任何帮助将不胜感激。
Any help will be appreciated.



您的选项包括:


a)将实施代码留在头文件中。这很常见。

练习。


b)使用支持export的编译器。这种情况并不常见,会让您的代码变得不那么便携。


最好


Kai-Uwe Bux


Your options include:

a) leave the implementation code in the header files. This is common
practice.

b) use a compiler that supports "export". This is not common and will render
you code less portable.

Best

Kai-Uwe Bux



感谢您的回复。我现在会选择a)。此致。

Thanks for your reply. I''ll go with option a) for now. Best regards.


2月9日下午6:54,aaragon < alejandro.ara ... @ gmail.comwrote:
On Feb 9, 6:54 pm, "aaragon" <alejandro.ara...@gmail.comwrote:

2月9日下午6:08,Kai-Uwe Bux< jkherci ... @ gmx .netwrote:
On Feb 9, 6:08 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:

aaragon写道:
aaragon wrote:

大家好,
Hi everyone,


我一直在写一些代码,到目前为止我已经用

编写了所有代码.h文件以避免链接器错误。我正在使用模板。

我想将实现移动到.cpp文件。在读完一些

之后,我发现唯一的方法是在.cpp文件中添加实际的

模板实例。但是,如果

你没有使用内置类型,你怎么做?例如,模板类

可能是,
I''ve been writing some code and so far I have all the code written in
the .h files in order to avoid the linker errors. I''m using templates.
I wanted to move the implementations to the .cpp files. After some
reading, I found that the only way to do this is to add the actual
template instantiations in the .cpp file. But, how do you do that if
you''re not working with built-in types? For example, a template class
might be,


模板

<

class Objective,

class StoragePolicy,

int k
template
<
class Objective,
class StoragePolicy,
int k


class ClassA {

...

//声明和定义

...

}
class ClassA {
...
// declarations and definitions
...
}


如果我的目标和

StoragePolicy由代码用户定义,如何将其分成.cpp和.h文件?
How do I separate this into .cpp and .h files if I Objective and
StoragePolicy are defined by the user of the code?


简答:你不是。
Short answer: you don''t.


在这个例子中,

StoragePolicy是我可以定义的东西(让'比方说

std :: vector),但Objective是用户提供的对象。

有办法解决这个问题吗?我还读了一些关于使用

" export"关键字,但它似乎不适用于gnu g ++。
In this example,
StoragePolicy is something that I could define (let''s say
std::vector), but Objective is an object that the user provides. Is
there a way to get around this? I read also something about using the
"export" keyword but it doesn''t seem to work with gnu g++.


export关键字用于此目的,但是,大多数编译器都不支持
支持它。
The export keyword is meant for this purpose, however, most compilers don''t
support it.


任何帮助将不胜感激。
Any help will be appreciated.


您的选项包括:
Your options include:


a)请假头文件中的实现代码。这是常见的

练习。
a) leave the implementation code in the header files. This is common
practice.


b)使用支持export的编译器。这种情况并不常见,并且会使代码变得不那么便携。
b) use a compiler that supports "export". This is not common and will render
you code less portable.


Best
Best


Kai-Uwe Bux
Kai-Uwe Bux



感谢您的回复。我现在会选择a)。最好的祝福。


Thanks for your reply. I''ll go with option a) for now. Best regards.



aaragon,


虽然这不是从#included标题中删除它,但这是一个很好的

将实现与文件中的定义分开的方法。


/////foo.hh


#ifndef FOO_HH

#define FOO_HH


模板< typename TYPE>

void

foo(TYPE val);


#include" foo.tcc"


#endif


////foo.tcc


#include< iostream>


模板< typename TYPE>

void

foo(TYPE val)

{

std :: cout<< ; val<< std :: endl;

}


////main.cc


#include" foo .hh"


int

main(int argc,char * argv [])

{

foo(1);

}


///结束示例。


HTH,


Paul Davis


aaragon,

Although this isn''t removing it from #included headers, this is a good
way to separate the implementation from definition in files.

/////foo.hh

#ifndef FOO_HH
#define FOO_HH

template< typename TYPE >
void
foo( TYPE val ) ;

#include "foo.tcc"

#endif

////foo.tcc

#include <iostream>

template< typename TYPE >
void
foo( TYPE val )
{
std::cout << val << std::endl ;
}

////main.cc

#include "foo.hh"

int
main( int argc, char* argv[] )
{
foo( 1 ) ;
}

///End of example.

HTH,

Paul Davis


这篇关于使用模板时将定义与声明分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆