avatar
谁给详细说一下这句# Programming - 葵花宝典
j*g
1
(int(*)(int const*))addValue
这两个指针是啥意思?
template原型是
template
T addValue(const T& x)
{ return x+VAL; }
avatar
t*t
2
addValue is a function (pointer); it has prototype "int addValue, 5>(const int&)".
cast it to another type of function pointer, which has prototype
"int xxxx(const int*)"
this is dangerous anyway...

【在 j****g 的大作中提到】
: (int(*)(int const*))addValue
: 这两个指针是啥意思?
: template原型是
: template
: T addValue(const T& x)
: { return x+VAL; }

avatar
j*g
3
我以为只能转换函数的返回类型呢。
怎么实现转换参数类型的呢?能详细说一下后台怎么处理的么?
这个问题来源于C++ template, writen by Davice Vandervoorde. Section 4.2
Nontype Function Template Parameters. It provides a function
std::transform(source.begin(), source.end(), dest.begin(), addValue)
It says that compiler may have problem doing parameter deduction in this
form, and gives another form:
std::transform(source.begin(), source.end(), dest.begin(),
(int(*)(int const*))addValue)
I'm totally lost about what he's talking about.

int

【在 t****t 的大作中提到】
: addValue is a function (pointer); it has prototype "int addValue: , 5>(const int&)".
: cast it to another type of function pointer, which has prototype
: "int xxxx(const int*)"
: this is dangerous anyway...

avatar
r*r
4
没看懂
不过我编译了一下,不强制转化也能通过VC2005

>)

【在 j****g 的大作中提到】
: 我以为只能转换函数的返回类型呢。
: 怎么实现转换参数类型的呢?能详细说一下后台怎么处理的么?
: 这个问题来源于C++ template, writen by Davice Vandervoorde. Section 4.2
: Nontype Function Template Parameters. It provides a function
: std::transform(source.begin(), source.end(), dest.begin(), addValue)
: It says that compiler may have problem doing parameter deduction in this
: form, and gives another form:
: std::transform(source.begin(), source.end(), dest.begin(),
: (int(*)(int const*))addValue)
: I'm totally lost about what he's talking about.

avatar
t*t
5
Read the holy standard
section 4.3, 13.4, and 5.2.10, clause 6
I suspect you have some typo, or he has, because the type of function is
changed. Calling a function through a different type of function pointer is,
well, you guessed, undefined. If the conversion is (int(*)(int const&))
addValue, then it makes perfect sense.
6 A pointer to a function can be explicitly converted to a pointer to a
function of a different type. The effect of calling a function
through a pointer to

【在 j****g 的大作中提到】
: 我以为只能转换函数的返回类型呢。
: 怎么实现转换参数类型的呢?能详细说一下后台怎么处理的么?
: 这个问题来源于C++ template, writen by Davice Vandervoorde. Section 4.2
: Nontype Function Template Parameters. It provides a function
: std::transform(source.begin(), source.end(), dest.begin(), addValue)
: It says that compiler may have problem doing parameter deduction in this
: form, and gives another form:
: std::transform(source.begin(), source.end(), dest.begin(),
: (int(*)(int const*))addValue)
: I'm totally lost about what he's talking about.

avatar
j*g
6
The standard only says about the return value, not the parameter a.
Correct me if I'm wrong.
avatar
t*t
7
did you read? guess not...

【在 j****g 的大作中提到】
: The standard only says about the return value, not the parameter a.
: Correct me if I'm wrong.

avatar
j*g
8
yeah, you are right.
Found another version, it was (int(*)(int const&))addValue
avatar
t*t
9
muhahahaha

【在 j****g 的大作中提到】
: yeah, you are right.
: Found another version, it was (int(*)(int const&))addValue

avatar
j*g
10
The original text is:
std::transform (source.begin(), source.end(), dest.begin(), addValue);
Note that there is a problem with this example: addValue is a
function template, and function templates are considered to name a set of
overloaded functions (even if the set has only one member). However,
according to the current standard, sets of overloaded functions cannot be
used for template parameter deduction. Thus, you have to cast to the exact
type of the function template argument:
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。