是否可以知道参数是否为默认值 [英] Is it possible to know if the parameter was defaulted
问题描述
警告:此问题仅限于MSVS
我有此函数签名:
void do_somthing(std::vector<foo>& bar={});
这两个调用函数可能不同:
Is it possible to differ between those two calls for the function:
第一个:
do_something()
第二:
std::vector<foo> v;
do_something(v);
换句话说,我想要的是:
In other words, I want something like:
void do_somthing(std::vector<foo>& bar={}){
if(/* bar was defaulted*/){
}
else{
}
}
EDIT:
实际代码:
The actual code:
template<class Tinput_iterator>
Tmodel perform_fitting(Tinput_iterator begin_data, Tinput_iterator end_data, std::vector<Tpoint>& inliers = {});
推荐答案
签名:
I have this function signature:
void do_somthing(std::vector<foo>& bar=std::vector<foo>{});
此无法编译危险的非标准编译器设置你应该远离。
This cannot compile, except with dangerous non-standard compiler settings you should stay away from.
特别是,Visual C ++允许这个如果 / Za
未指定,但 / W4
仍会产生如下警告:
In particular, Visual C++ allows this if /Za
is not specified, but with /W4
still produces a warning like this:
stackoverflow.cpp(6): warning C4239: nonstandard extension used: 'default argument': conversion from 'std::vector<foo,std::allocator<_Ty>>' to 'std::vector<foo,
std::allocator<_Ty>> &'
with
[
_Ty=foo
]
stackoverflow.cpp(6): note: A non-const reference may only be bound to an lvalue
void do_somthing(std::vector<foo>& bar=std::vector<foo>{}){
if(/* bar was defaulted*/){
}
else{
}
}
即使我们假设你实际上包含了缺少的 const
来编译代码,答案是:不,不可能知道 bar
是否为默认值。
Even if we assume that you actually included the missing const
to make the code compile, the answer would be: no, it is not possible to know if bar
was defaulted.
无论您打算在这里做什么,都必须找到完全不同的解决方案。
Whatever you plan to do here, you have to find a completely different solution.
这篇关于是否可以知道参数是否为默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!