函数指针和模板 [英] function pointes and templates
问题描述
有谁可以解释为什么这段代码无法编译?
#include< iostream>
void func()
{
std :: cout<< 成功!\ n;
}
struct S
{
模板< class F>
void方法(const F& f)
{
f();
}
};
int main()
{
s s;
s.method(func);
}
但主要改动很小
int main()
{
s s;
s.method(& func); //更改此处
}
它编译并运行。
john
Can anyone explain why this code fails to compile?
#include <iostream>
void func()
{
std::cout << "success!\n";
}
struct S
{
template <class F>
void method(const F& f)
{
f();
}
};
int main()
{
S s;
s.method(func);
}
but with a very slight change to main
int main()
{
S s;
s.method(&func); // change here
}
it compiles and works.
john
推荐答案
2004年4月6日星期二18:19:19 +0100,John Harrison
< jo ****** *******@hotmail.com>写道:
On Tue, 6 Apr 2004 18:19:19 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:
有谁可以解释为什么这段代码无法编译?
Can anyone explain why this code fails to compile?
[snip]
>
看起来像编译器错误。我的工作正常。
Tom
-
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C常见问题解答: http://www.eskimo.com /〜scs/C-faq/top.html
[snip]
Looks like a compiler bug. It works fine on mine.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
2004年4月6日星期二18:19:19 +0100,John Harrison
< jo ************* @ hotmail.com>写道:
On Tue, 6 Apr 2004 18:19:19 +0100, "John Harrison"
<jo*************@hotmail.com> wrote:
有谁可以解释为什么这段代码无法编译?
Can anyone explain why this code fails to compile?
[snip]
>
看起来像编译器错误。我的工作正常。
Tom
-
C ++ FAQ: http://www.parashift.com/c++-faq-lite/
C常见问题解答: http://www.eskimo.com /〜scs/C-faq/top.html
[snip]
Looks like a compiler bug. It works fine on mine.
Tom
--
C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
John Harrison写道:
John Harrison wrote:
有谁可以解释为什么这段代码无法编译?
#include< iostream>
void func()
{
std :: cout<< 成功!\ n;
}
{
模板< class F>
void方法(const F& f )
{
f();
}
};
int main()
{s / s;
s.method(func);
}
但主要的改动很轻微
{
s s;
s.method(& func); //改变这里
它编译和工作。
约翰
Can anyone explain why this code fails to compile?
#include <iostream>
void func()
{
std::cout << "success!\n";
}
struct S
{
template <class F>
void method(const F& f)
{
f();
}
};
int main()
{
S s;
s.method(func);
}
but with a very slight change to main
int main()
{
S s;
s.method(&func); // change here
}
it compiles and works.
john
我认为它因为一个函数(不是一个对象)不能是const,
如图所示,它在我的系统上编译:
void f (){}
int main()
{
typedef void(& func_t)();
const func_t f = func;
func_t g = f; //如果g'的指示对象真的是常量,那就不行了。
}
因此,你不能初始化参考 - to-const-T有一个函数,
因为函数可以是T而不是const T.
指针指向的情况也是如此功能。
-
问候,
巴斯特。
I think it''s because a function (not being an object) cannot be const,
as illustrated by this, which does compile on my system:
void f () { }
int main ()
{
typedef void (& func_t) ();
const func_t f = func;
func_t g = f; // If g''s referent were really const this wouldn''t work.
}
Therefore, you can''t initialize a reference-to-const-T with a function,
since a function can be T but not const T.
The same is not true of a pointer-to-function.
--
Regards,
Buster.
这篇关于函数指针和模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!