命名空间与模板一起使用? [英] Namespace goes with template?

查看:104
本文介绍了命名空间与模板一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在main.cpp中定义了名称空间hpc,所以不要与其他

库冲突。但我发现,在命名空间boo中,使用命名空间hpc中的类实例化一个

模板,会导致编译错误,因为这两个命名空间中的函数会发生冲突。这是编译器错误吗?或者

预期的C ++行为?


感谢任何人可以为我点灯。


见下面:


func.h:在函数''void boo :: rfunc(const boo :: R< T>&)中[含T =

hpc :: B]'':

main.cpp:16:从这里实例化

func.h:13:错误:调用重载''工作(const boo) :: R< hpc :: B>&)''是

暧昧

func.h:5:注意:候选人是:void boo :: work(T )[与T =

boo :: R< hpc :: B>]

main.cpp:23:注意:void hpc :: work(T)[with T =

boo :: R< hpc :: B>]

func.h:

namespace boo {

模板< typename T>

void work(T n){std :: cout<< 再见的工作 <<的std :: ENDL; }

模板< typename Tclass R {};

模板< typename T>

void rfunc(const R< T>& a){work(a); }

} //名称空间boo


main.cpp:

#include< iostream>

#include" func.h"

使用std :: cout;

使用std :: endl;

namespace hpc {

B级{};

A级{

公开:

void bfunc(void){rfunc(n ); }

受保护:

boo :: R< Bn;

};


模板< ; typename T>

void work(T n){cout<< 你好世界的工作 << ENDL;使用hpc :: A;
使用hpc :: A;
char * argv [])

{

A a;

a.bfunc();

返回0 ;

}

I defined namespace hpc in main.cpp, so not to clash with other
libraries. But I found that, in namespace boo, instantiating a
template with a class in namespace hpc, causes compilation errors by
clashing the functions in the two namespaces. Is it a compiler bug? or
intended C++ behavior?

Appreciate if any one can shed some lights for me.

See following:

func.h: In function ''void boo::rfunc(const boo::R<T>&) [with T =
hpc::B]'':
main.cpp:16: instantiated from here
func.h:13: error: call of overloaded ''work(const boo::R<hpc::B>&)'' is
ambiguous
func.h:5: note: candidates are: void boo::work(T) [with T =
boo::R<hpc::B>]
main.cpp:23: note: void hpc::work(T) [with T =
boo::R<hpc::B>]

func.h:
namespace boo {
template <typename T>
void work(T n) { std::cout << "good bye work" << std::endl; }
template <typename Tclass R { };
template <typename T>
void rfunc(const R<T>& a) { work(a); }
} // namespace boo

main.cpp:
#include <iostream>
#include "func.h"
using std::cout;
using std::endl;
namespace hpc {
class B { };
class A {
public:
void bfunc(void) { rfunc(n); }
protected:
boo::R<Bn;
};

template <typename T>
void work(T n) { cout << "hello world work" << endl; }

} // namespace hpc

using hpc::A;
int main(int argc, char* argv[])
{
A a;
a.bfunc();
return 0;
}

推荐答案

xman写道:
xman wrote:

我在main.cpp中定义了名称空间hpc,所以不要与其他

库冲突。但我发现,在命名空间boo中,使用命名空间hpc中的类实例化一个

模板,会导致编译错误,因为这两个命名空间中的函数会发生冲突。这是编译器错误吗?或者

预期的C ++行为?
I defined namespace hpc in main.cpp, so not to clash with other
libraries. But I found that, in namespace boo, instantiating a
template with a class in namespace hpc, causes compilation errors by
clashing the functions in the two namespaces. Is it a compiler bug? or
intended C++ behavior?



我相信它是有意的。

I believe it''s intended.


感谢任何人可以为我照亮一些灯光。


见下面:


func.h:在函数''void boo :: rfunc(const boo :: R< T> &)[与T =

hpc :: B]'':

main.cpp:16:从这里实例化

func。 h:13:错误:调用重载''工作(const boo :: R< hpc :: B>&)''是

不明确

func.h :5:注意:候选人是:void boo :: work(T)[with T =

boo :: R< hpc :: B>]

main.cpp :23:注意:void hpc :: work(T)[with T =

boo :: R< hpc :: B>]
Appreciate if any one can shed some lights for me.

See following:

func.h: In function ''void boo::rfunc(const boo::R<T>&) [with T =
hpc::B]'':
main.cpp:16: instantiated from here
func.h:13: error: call of overloaded ''work(const boo::R<hpc::B>&)'' is
ambiguous
func.h:5: note: candidates are: void boo::work(T) [with T =
boo::R<hpc::B>]
main.cpp:23: note: void hpc::work(T) [with T =
boo::R<hpc::B>]



我认为你遇到了一个ADL案例(Argument-Dependent name

Lookup)。要确定要调用哪个函数,允许编译器使用
来查看参数(以及模板参数)的名称空间。在

你的情况下,因为在实例化''boo :: rfunc< T>''时,''R''来自''boo''

和'' T''(推断为''hpc :: B'')来自''hpc''命名空间。这两个命名空间

被认为是使工作符号模糊不清。

I think you''re running into a case of ADL (Argument-Dependent name
Lookup). To figure out which function to call, the compiler is allowed
to look in the namespaces of arguments (and of template arguments). In
your case, since when instantiating ''boo::rfunc<T>'', ''R'' is from ''boo''
and ''T'' (deduced as ''hpc::B'') is from ''hpc'' namespace. Both namespaces
are considered making ''work'' symbol ambiguous.


>

func.h :

名称空间boo {

模板< typename T>

void work(T n){std :: cout<< 再见的工作 <<的std :: ENDL; }

模板< typename Tclass R {};

模板< typename T>

void rfunc(const R< T>& a){work(a); }

} //名称空间boo


main.cpp:

#include< iostream>

#include" func.h"
>
func.h:
namespace boo {
template <typename T>
void work(T n) { std::cout << "good bye work" << std::endl; }
template <typename Tclass R { };
template <typename T>
void rfunc(const R<T>& a) { work(a); }
} // namespace boo

main.cpp:
#include <iostream>
#include "func.h"



附注:这里你包含''func.h''取决于

之前包含的< iostream> 。最好避免这种情况。考虑

包括< iostreamin''func.h'',并且仍然保留在这里因为

这个模块使用''cout''和''endl''作为好吧。

A side note: here you made inclusion of ''func.h'' dependent on the
previous inclusion of <iostream>. Better to avoid this. Consider
including <iostreamin ''func.h'', and still keep it here because
this module uses ''cout'' and ''endl'' as well.


使用std :: cout;

使用std :: endl;

namespace hpc {

B级{};

A级{

公开:

void bfunc(void){rfunc(n ); }

受保护:

boo :: R< Bn;

};


模板< ; typename T>

void work(T n){cout<< 你好世界的工作 << ENDL;使用hpc :: A;
使用hpc :: A;
char * argv [])

{

A a;

a.bfunc();

返回0 ;

}
using std::cout;
using std::endl;
namespace hpc {
class B { };
class A {
public:
void bfunc(void) { rfunc(n); }
protected:
boo::R<Bn;
};

template <typename T>
void work(T n) { cout << "hello world work" << endl; }

} // namespace hpc

using hpc::A;
int main(int argc, char* argv[])
{
A a;
a.bfunc();
return 0;
}



-

请在回复e-时删除资金'A'邮件

我没有回复最热门的回复,请不要问

--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


你好Victor,

奇怪的是,我试图使用使用boo :: work,假设它应该解决

模糊名称查找,但它仍会产生相同的错误。


模板< typename T>

void rfunc(const R< T>& a){

使用boo :: work;

work(a);

}


感谢分享您的想法。


5月7日晚9点31分,Victor Bazarov和< v.Abaza ... @ comAcast.netwrote:
Hi Victor,

Strangely, I tried to use "using boo::work", suppose it should resolve
ambiguous name look up, yet it still produces the same error.

template <typename T>
void rfunc(const R<T>& a) {
using boo::work;
work(a);
}

Thanks for sharing your thoughts.

On May 7, 9:31 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

xman写道:
xman wrote:

我定义main.cpp中的命名空间hpc,所以不要与其他

库冲突。但我发现,在命名空间boo中,使用命名空间hpc中的类实例化一个

模板,会导致编译错误,因为这两个命名空间中的函数会发生冲突。这是编译器错误吗?或者

预期的C ++行为?
I defined namespace hpc in main.cpp, so not to clash with other
libraries. But I found that, in namespace boo, instantiating a
template with a class in namespace hpc, causes compilation errors by
clashing the functions in the two namespaces. Is it a compiler bug? or
intended C++ behavior?



我相信它是有意的。


I believe it''s intended.


感谢任何人可以为我点灯。
Appreciate if any one can shed some lights for me.


请参阅以下内容:
See following:


func.h:in function' 'void boo :: rfunc(const boo :: R< T>&)[with T =

hpc :: B]'':

main.cpp: 16:从这里实例化

func.h:13:错误:调用重载''工作(const boo :: R< hpc :: B>&)''是

暧昧

func.h:5:注意:候选人是:void boo :: work(T)[with T =

boo :: R< hpc: :B>]

main.cpp:23:注意:void hpc :: work(T)[with T =

boo :: R< hpc :: B> ]
func.h: In function ''void boo::rfunc(const boo::R<T>&) [with T =
hpc::B]'':
main.cpp:16: instantiated from here
func.h:13: error: call of overloaded ''work(const boo::R<hpc::B>&)'' is
ambiguous
func.h:5: note: candidates are: void boo::work(T) [with T =
boo::R<hpc::B>]
main.cpp:23: note: void hpc::work(T) [with T =
boo::R<hpc::B>]



我认为你遇到了一个ADL案例(Argument-Dependent name

Lookup)。要确定要调用哪个函数,允许编译器使用
来查看参数(以及模板参数)的名称空间。在

你的情况下,因为在实例化''boo :: rfunc< T>''时,''R''来自''boo''

和'' T''(推断为''hpc :: B'')来自''hpc''命名空间。两个名称空间

被认为是使工作符号模糊不清。


I think you''re running into a case of ADL (Argument-Dependent name
Lookup). To figure out which function to call, the compiler is allowed
to look in the namespaces of arguments (and of template arguments). In
your case, since when instantiating ''boo::rfunc<T>'', ''R'' is from ''boo''
and ''T'' (deduced as ''hpc::B'') is from ''hpc'' namespace. Both namespaces
are considered making ''work'' symbol ambiguous.


func.h:

名称空间boo {

template< typename T>

void work(T n){std :: cout<< 再见的工作 <<的std :: ENDL; }

模板< typename Tclass R {};

模板< typename T>

void rfunc(const R< T>& a){work(a); }

} //名称空间boo
func.h:
namespace boo {
template <typename T>
void work(T n) { std::cout << "good bye work" << std::endl; }
template <typename Tclass R { };
template <typename T>
void rfunc(const R<T>& a) { work(a); }
} // namespace boo


main.cpp:

#include< iostream>

#include" func.h"
main.cpp:
#include <iostream>
#include "func.h"



附注:这里你包含''func.h'',取决于

之前包含的< iostream> 。最好避免这种情况。考虑

包括< iostreamin''func.h'',并且仍然保留在这里因为

这个模块使用''cout''和''endl''作为好吧。


A side note: here you made inclusion of ''func.h'' dependent on the
previous inclusion of <iostream>. Better to avoid this. Consider
including <iostreamin ''func.h'', and still keep it here because
this module uses ''cout'' and ''endl'' as well.


使用std :: cout;

使用std :: endl;

namespace hpc {

B级{};

A级{

公开:

void bfunc(void){rfunc(n ); }

受保护:

boo :: R< Bn;

};
using std::cout;
using std::endl;
namespace hpc {
class B { };
class A {
public:
void bfunc(void) { rfunc(n); }
protected:
boo::R<Bn;
};


template< typename T>

void work(T n){cout<< 你好世界的工作 << ENDL; }
template <typename T>
void work(T n) { cout << "hello world work" << endl; }


} //名称空间hpc
} // namespace hpc


使用hpc :: A;

int main(int argc,char * argv [])

{

A a;

a.bfunc();

返回0;

}
using hpc::A;
int main(int argc, char* argv[])
{
A a;
a.bfunc();
return 0;
}



-

请在通过电子邮件回复时删除大写''A'

我没有回复热门回复,请不要问


--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



你好Victor,


奇怪的是,我试图使用使用boo :: work,假设它应该解决

模糊名称查找,但它仍会产生相同的错误。


模板< typename T>

void rfunc(const R< ; T>& a){

使用boo :: work;

work(a);

}


感谢分享您的想法。


5月7日晚上9:31,Victor Bazarov& QUOT; < v.Abaza ... @ comAcast.netwrote:
Hi Victor,

Strangely, I tried to use "using boo::work", suppose it should resolve
ambiguous name look up, yet it still produces the same error.

template <typename T>
void rfunc(const R<T>& a) {
using boo::work;
work(a);
}

Thanks for sharing your thoughts.

On May 7, 9:31 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

xman写道:
xman wrote:

我定义main.cpp中的命名空间hpc,所以不要与其他

库冲突。但我发现,在命名空间boo中,使用命名空间hpc中的类实例化一个

模板,会导致编译错误,因为这两个命名空间中的函数会发生冲突。这是编译器错误吗?或者

预期的C ++行为?
I defined namespace hpc in main.cpp, so not to clash with other
libraries. But I found that, in namespace boo, instantiating a
template with a class in namespace hpc, causes compilation errors by
clashing the functions in the two namespaces. Is it a compiler bug? or
intended C++ behavior?



我相信它是有意的。


I believe it''s intended.


感谢任何人可以为我点灯。
Appreciate if any one can shed some lights for me.


请参阅以下内容:
See following:


func.h:in function' 'void boo :: rfunc(const boo :: R< T>&)[with T =

hpc :: B]'':

main.cpp: 16:从这里实例化

func.h:13:错误:调用重载''工作(const boo :: R< hpc :: B>&)''是

暧昧

func.h:5:注意:候选人是:void boo :: work(T)[with T =

boo :: R< hpc: :B>]

main.cpp:23:注意:void hpc :: work(T)[with T =

boo :: R< hpc :: B> ]
func.h: In function ''void boo::rfunc(const boo::R<T>&) [with T =
hpc::B]'':
main.cpp:16: instantiated from here
func.h:13: error: call of overloaded ''work(const boo::R<hpc::B>&)'' is
ambiguous
func.h:5: note: candidates are: void boo::work(T) [with T =
boo::R<hpc::B>]
main.cpp:23: note: void hpc::work(T) [with T =
boo::R<hpc::B>]



我认为你遇到了一个ADL案例(Argument-Dependent name

Lookup)。要确定要调用哪个函数,允许编译器使用
来查看参数(以及模板参数)的名称空间。在

你的情况下,因为在实例化''boo :: rfunc< T>''时,''R''来自''boo''

和'' T''(推断为''hpc :: B'')来自''hpc''命名空间。两个名称空间

被认为是使工作符号模糊不清。


I think you''re running into a case of ADL (Argument-Dependent name
Lookup). To figure out which function to call, the compiler is allowed
to look in the namespaces of arguments (and of template arguments). In
your case, since when instantiating ''boo::rfunc<T>'', ''R'' is from ''boo''
and ''T'' (deduced as ''hpc::B'') is from ''hpc'' namespace. Both namespaces
are considered making ''work'' symbol ambiguous.


func.h:

名称空间boo {

template< typename T>

void work(T n){std :: cout<< 再见的工作 <<的std :: ENDL; }

模板< typename Tclass R {};

模板< typename T>

void rfunc(const R< T>& a){work(a); }

} //名称空间boo
func.h:
namespace boo {
template <typename T>
void work(T n) { std::cout << "good bye work" << std::endl; }
template <typename Tclass R { };
template <typename T>
void rfunc(const R<T>& a) { work(a); }
} // namespace boo


main.cpp:

#include< iostream>

#include" func.h"
main.cpp:
#include <iostream>
#include "func.h"



附注:这里你包含''func.h'',取决于

之前包含的< iostream> 。最好避免这种情况。考虑

包括< iostreamin''func.h'',并且仍然保留在这里因为

这个模块使用''cout''和''endl''作为好吧。


A side note: here you made inclusion of ''func.h'' dependent on the
previous inclusion of <iostream>. Better to avoid this. Consider
including <iostreamin ''func.h'', and still keep it here because
this module uses ''cout'' and ''endl'' as well.


使用std :: cout;

使用std :: endl;

namespace hpc {

B级{};

A级{

公开:

void bfunc(void){rfunc(n ); }

受保护:

boo :: R< Bn;

};
using std::cout;
using std::endl;
namespace hpc {
class B { };
class A {
public:
void bfunc(void) { rfunc(n); }
protected:
boo::R<Bn;
};


template< typename T>

void work(T n){cout<< 你好世界的工作 << ENDL; }
template <typename T>
void work(T n) { cout << "hello world work" << endl; }


} //名称空间hpc
} // namespace hpc


使用hpc :: A;

int main(int argc,char * argv [])

{

A a;

a.bfunc();

返回0;

}
using hpc::A;
int main(int argc, char* argv[])
{
A a;
a.bfunc();
return 0;
}



-

请在通过电子邮件回复时删除大写''A'

我没有回复热门回复,请不要问


--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask



这篇关于命名空间与模板一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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