“功能模板”问题 [英] "Function template" problem
问题描述
问候,
我无法弄清楚为什么以下代码无法编译:
--- BEGIN CODE ---
typedef double ftype(double);
struct A
{
double x;
模板< ftype F>
双eval()
{
返回F (x);
}
};
模板< ftype F>
struct B
{
A a;
double foo()
{
返回a.eval< F>();
}
};
---结束代码 - -
它生成错误消息:
scratch.cpp:在成员函数`double B< F> :: foo()'':
scratch.cpp:23:错误:在';''之前解析错误令牌
[第23行返回a.eval< F>(); ]
Win2K编译器gcc(GCC)3.3.3(cygwin特别版)
任何帮助表示赞赏,
>
-
Lionel B
Greetings,
I cannot figure out why the following code does not compile:
--- BEGIN CODE ---
typedef double ftype(double);
struct A
{
double x;
template<ftype F>
double eval()
{
return F(x);
}
};
template<ftype F>
struct B
{
A a;
double foo()
{
return a.eval<F>();
}
};
--- END CODE ---
It generates the error message:
scratch.cpp: In member function `double B<F>::foo()'':
scratch.cpp:23: error: parse error before `;'' token
[ line 23 being return a.eval<F>(); ]
Compiler gcc (GCC) 3.3.3 (cygwin special) on Win2K
Any help appreciated,
--
Lionel B
推荐答案
" Lionel B" ; <去**** @ lionelb.com> schrieb im Newsbeitrag
新闻:e1 ************************** @ posting.google.c om ...
"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
问候,
我无法弄清楚为什么以下代码无法编译:
---开始代码---
typedef double ftype(double);
你所做的是:
模板< double f> double func(fx);
但是:你不想给出模板参数的类型,因为
它是'模板'。
所以,使用
模板< class C> double fkt(C argC);
struct A
{x />双x;
模板< ftype F>
double eval()
{
返回F(x);
}
};
模板< ftype F>
struct B
{
A a;
双foo()
{
返回a.eval< F>();
}
};
---结束代码---
它生成错误信息:
scratch.cpp:在成员函数`double B< F> :: foo( )'':
scratch.cpp:23:错误:在';''之前解析错误令牌
[第23行返回a.eval< F>(); ]
关于Win2K的编译器gcc(GCC)3.3.3(cygwin特别版)
任何帮助表示赞赏,
-
Lionel B
Greetings,
I cannot figure out why the following code does not compile:
--- BEGIN CODE ---
typedef double ftype(double);
what you do is:
template <double f> double func(f x);
But: You don''t want to give the type of the template argument, since
it''s a "template".
So, use
template<class C> double fkt(C argC);
struct A
{
double x;
template<ftype F>
double eval()
{
return F(x);
}
};
template<ftype F>
struct B
{
A a;
double foo()
{
return a.eval<F>();
}
};
--- END CODE ---
It generates the error message:
scratch.cpp: In member function `double B<F>::foo()'':
scratch.cpp:23: error: parse error before `;'' token
[ line 23 being return a.eval<F>(); ]
Compiler gcc (GCC) 3.3.3 (cygwin special) on Win2K
Any help appreciated,
--
Lionel B
Gernot Frisch写道:
Gernot Frisch wrote:
" Lionel B" <去**** @ lionelb.com> schrieb im Newsbeitrag
新闻:e1 ************************** @ posting.google.c om ...
"Lionel B" <go****@lionelb.com> schrieb im Newsbeitrag
news:e1**************************@posting.google.c om...
问候,
我无法弄清楚为什么以下代码无法编译:
---开始代码---
typedef double ftype(double);
Greetings,
I cannot figure out why the following code does not compile:
--- BEGIN CODE ---
typedef double ftype(double);
你做的是:
模板< double f> double func(fx);
但是:你不想给出模板参数的类型,因为
它是一个模板。
所以,使用
what you do is:
template <double f> double func(f x);
But: You don''t want to give the type of the template argument, since
it''s a "template".
So, use
template<class C> double fkt(C argC);
道歉,如果我迟钝了,但我根本不理解这个回复
:(
-
Lionel B
Apologies if I''m being obtuse, but I don''t understand this reply at all
:(
--
Lionel B
您好>
Lionel B写道:
Hi
Lionel B wrote:
double foo()
{
返回a.eval< F>();
}
};
---结束代码---
它会生成错误信息:
scratch.cpp:在成员函数中`double B< F> :: foo()'':
scratch.cpp:23:错误:在';''令牌之前解析错误
double foo()
{
return a.eval<F>();
}
};
--- END CODE ---
It generates the error message:
scratch.cpp: In member function `double B<F>::foo()'':
scratch.cpp:23: error: parse error before `;'' token
这是一种极少数情况,你需要告诉编译器
名称eval是一个成员模板(它是一个依赖名称),否则''<''
被视为小于。
double foo()
{
返回a.template eval< F>();
}
Markus
This is one of the rare cases where you need to tell the compiler that the
name eval is a member template (it''s a dependent name), otherwise the ''<''
is considered "less than".
double foo()
{
return a.template eval<F>();
}
Markus
这篇关于“功能模板”问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!