模板朋友功能注入 [英] Template friend function injection

查看:65
本文介绍了模板朋友功能注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于在模板中注入朋友功能的问题

类。我的问题是特定于gcc(版本3.4.5)在

与mingw的组合中使用,因为这段代码(或至少代码得到

相同的结果)按预期工作在visualc ++中。我知道这是

可能不是编译器的正确行为,但它是我正在寻找的那种

行为所以我希望有在gcc中使用

相同的方法。

正如您所知,当您定义一个类(没有模板)并且在

内时你定义了一个友元函数的类,该函数直接在类本身外部注入

范围。


---代码---


void函数();


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

{

函数(); //这个有效

}


等级测试

{

公开:

friend void函数(){printf(" Function()");的getchar(); }

};


- 结束代码 -

现在我想模仿模板类的相同行为,但是

这段代码不起作用:


- 代码 -


void Function() ;


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

{

函数(); //这不起作用

}


模板< typename T>

类测试

{

public:

friend void Function(){printf(" Function()");的getchar(); }

};


模板类测试< int> ;;


- 结束代码 -


具体来说,我在链接时收到一个关于Function()的错误,而不是定义
,我猜这是因为编译器不考虑

函数的定义是一个非模板函数而是

假设它是一个模板函数,因为它是在模板类的
里面定义的。

现在,有趣的是,如果你移动main()以下的模板类Test的显式

实例化,这段代码实际编译了

并且有效,这就是为什么我认为它是一个错误的原因。因为

理论上如果Function()的定义是模板函数

(因此不是正常函数)它永远不应该用于调用
$ b main()函数内部的$ b,无论main()函数是否低于实例化的b $ b。我是对的吗?

无论如何我正在寻找一种方法来定义函数()

可用于main()函数,即使main()也是如此函数在模板实例化之上。

。有没有办法为gcc实现这个?

像某些命令行标志或什么?我真的很感激。

谢谢。

解决方案

H9 ************** @ spambox.us 写道:


- 代码 -


void函数();


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

{

函数(); //这不起作用

}



显然你在这里调用函数的外部版本。


template< typename T>

class Test

{

public:

friend void Function(){printf(" Function()");的getchar(); }



现在定义函数内联。这是一个错误。


};


模板类测试< int> ;;


- 结束代码 -


特别是我在链接时收到关于Function()的错误

被定义,我猜是由于编译器没有考虑将函数的定义设为非模板函数而是

假设它是模板函数只是因为它是在模板类的
里面定义的。



编号因为到目前为止函数被声明为extern,这显然是错误的。


现在,有趣的是,如果你将main()移动到模板类Test的显式

实例化,这段代码实际编译了

和工作,这就是为什么我认为它是一个错误的原因。因为

理论上如果Function()的定义是模板函数

(因此不是正常函数)它永远不应该用于调用
$ b main()函数内部的$ b,无论main()函数是否低于实例化的b $ b。我对吗?



否。第二种方法是有效代码。在内联定义后,你调用Function



无论如何我正在寻找一种方法来定义Function()

可用于main()函数,即使main()函数高于

模板实例化。有没有办法为gcc实现这个目标?



必须在使用之前定义内联函数。围绕这个没有办法



我更喜欢在Test体外定义Function。像那样:

void函数();


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

{

函数(); //这不起作用

}


模板< typename T>

类测试

{

public:

friend void功能();

};


模板类测试< int> ;;


void函数()

{printf(" Function()");的getchar(); }

Marcel


MarcelMüller写道:


[..]

必须在使用之前定义内联函数。这个没有办法



[..]



所以,你说的是以下不应编译/链接?


int foo();

int main()

{

返回foo();

}

inline int foo(){return 0; } //注意''内联''


V

-

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

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


7月11日,5日:37 * pm,MarcelMüller< news.5.ma ... @ spamgourmet.org>

写道:


>

显然你在这里调用函数的外部版本。


template< typename T>

class测试

{

public:

* * friend void Function(){printf(" Function()");的getchar(); }



现在定义函数内联。这是一个错误。



很抱歉,如果我在模板中定义了

,那么为什么函数被定义为内联函数,它是如果我在正常的类中定义它不是吗?


>

否。这是因为函数被声明为到目前为止extern是

显然是错的。



嗯,那么这个页面是错误的(或者至少令人困惑):
http://www.parashift.com/ c ++ - faq-lit ... html#faq-35.16

它说:

"在编译器看到那些神奇的东西之后,它会更好告知

关于朋友的功能。特别是,它会意识到

的朋友行是指自己模板的功能。

这可以消除混乱。


另一种方法是在你声明它是朋友的同时在类中定义友元函数

body。


>

否。第二种方法是有效代码。你调用函数后,

内联定义。



那么它意味着第一部分中的代码(当函数

函数()在非模板类中定义)应该是错的

,因此编译器/链接器发出信号,不是吗?
< blockquote class =post_quotes>
>

必须在使用之前定义内联函数。围绕这个没有办法



我更喜欢在Test体外定义Function。就像那样:


void函数();


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

{

* * * *功能(); //这不起作用


}


模板< typename T>

class Test

{

public:

* * * * friend void Function();


};


模板类测试< int> ;;


void函数()

{printf(" Function()" );的getchar(); }


Marcel



不幸的是我不能这样做因为这种方法的全部目的

是在Function()函数内部做一些事情。使用

模板,以便在有人使用某个模板

参数实例化Test类时,仅在最终的
时定义该函数。 br />


Hi, I have a question about injecting friend functions within template
classes. My question is specific to gcc (version 3.4.5) used in
combination with mingw because this code (or at least code that gets
the same result) works as expected in visualc++. I know that this is
probably not the right behavior for a compiler but it''s the kind of
behavior I''m searching for so I was hoping there was a way to do the
same thing in gcc.
As you know when you define a class (with no template) and within the
class you define a friend function, the function is injected in the
scope directly outside of the class itself.

--- code ---

void Function();

int main(int argc, char* argv[])
{
Function(); // This works
}

class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};

-- end code --
Now I would like to mimic the same behavior with template classes, but
this code doesn''t work:

-- code --

void Function();

int main(int argc, char* argv[])
{
Function(); // This does not work
}

template <typename T>
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }
};

template class Test<int>;

-- end code --

Specifically I receive an error at link time about Function() not
being defined, which I guess is due to the compiler not considering
the definition of Function to be a non-template function and instead
assuming it to be a template function just because it''s defined inside
of a template class.
Now, the funny thing is that if you move the main() BELOW the explicit
instantiation of the template class Test, this code actually compile
and works, that''s why I consider it to be a "bug" because
theoretically if the definition of Function() is a template function
(thus not a normal function) it should never be used for the call
inside of the main() function, whether the main() function is below
the instantiation or not. Am I right?
Anyway I''m searching for a way to make the definition of Function()
available for the main() function even if the main() function is above
the template instantiation. Is there a way to accomplish this for gcc?
Like some command-line flag or something? I''d really appreciate that.
Thanks.

解决方案

H9**************@spambox.us wrote:

-- code --

void Function();

int main(int argc, char* argv[])
{
Function(); // This does not work
}

Obviously you are calling an external version of Function here.

template <typename T>
class Test
{
public:
friend void Function() { printf("Function()"); getchar(); }

Now you define Function inline. this is an error.

};

template class Test<int>;

-- end code --

Specifically I receive an error at link time about Function() not
being defined, which I guess is due to the compiler not considering
the definition of Function to be a non-template function and instead
assuming it to be a template function just because it''s defined inside
of a template class.

No. It is because Function is declared as extern so far which is
obviously wrong.

Now, the funny thing is that if you move the main() BELOW the explicit
instantiation of the template class Test, this code actually compile
and works, that''s why I consider it to be a "bug" because
theoretically if the definition of Function() is a template function
(thus not a normal function) it should never be used for the call
inside of the main() function, whether the main() function is below
the instantiation or not. Am I right?

No. The second approach is valid code. You call Function after it is
defined inline.

Anyway I''m searching for a way to make the definition of Function()
available for the main() function even if the main() function is above
the template instantiation. Is there a way to accomplish this for gcc?

Inline functions must be defined before their use. There is no way
around this.
I would prefer to define Function outside the body of Test. Like that:
void Function();

int main(int argc, char* argv[])
{
Function(); // This does not work
}

template <typename T>
class Test
{
public:
friend void Function();
};

template class Test<int>;

void Function()
{ printf("Function()"); getchar(); }
Marcel


Marcel Müller wrote:

[..]
Inline functions must be defined before their use. There is no way
around this.
[..]

So, you''re saying that the following should not compile/link?

int foo();
int main()
{
return foo();
}
inline int foo() { return 0; } // note the ''inline''

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


On Jul 11, 5:37*pm, Marcel Müller <news.5.ma...@spamgourmet.org>
wrote:

>
Obviously you are calling an external version of Function here.

template <typename T>
class Test
{
public:
* *friend void Function() { printf("Function()"); getchar(); }


Now you define Function inline. this is an error.

Sorry, but why the function is defined as inline if I define it inside
of a template and it is not if I define it inside of a normal class?

>
No. It is because Function is declared as extern so far which is
obviously wrong.

Well, then this page is wrong (or at least confusing):
http://www.parashift.com/c++-faq-lit...html#faq-35.16
It says:
"After the compiler sees that magic stuff, it will be better informed
about the friend functions. In particular, it will realize that the
friend lines are referring to functions that are themselves templates.
That eliminates the confusion.

Another approach is to define the friend function within the class
body at the same moment you declare it to be a friend."

>
No. The second approach is valid code. You call Function after it is
defined inline.

Then it means that the code in the first section (when the function
Function() is defined within the non-template class) should be wrong
too, and thus signaled by the compiler/linker, isn''t it?

>
Inline functions must be defined before their use. There is no way
around this.
I would prefer to define Function outside the body of Test. Like that:

void Function();

int main(int argc, char* argv[])
{
* * * * Function(); // This does not work

}

template <typename T>
class Test
{
public:
* * * * friend void Function();

};

template class Test<int>;

void Function()
{ printf("Function()"); getchar(); }

Marcel

Unluckily I can''t do that because the whole purpose of this approach
was to do something with T inside of the Function() function. Using
templates so that the function is defined only at the very final
moment when someone instantiate the Test class with some template
argument.


这篇关于模板朋友功能注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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