如何在不使用 C++0x auto 的情况下实现这种类型特定的对象生成器
我有一个模板化函数,其中模板化参数是一个函子。我想生成一些特定于特定质量函数的内部变量,供从此函子调用的其他模板函数使用。所以我尝试了 auto 关键字,它似乎可以工作,
template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
auto internal_variables = gen(quality_function);
float result = do_some_work(internal_variables);
...
return result;
}
struct internals_for_linearised_functor {
double x;
};
internals_for_linearised_functor gen(mynamespace::linearised_function &f) {
linearised_internals internals;
internals.x = 5;
return internals;
}
使用 auto 似乎编译得很好,这令人高兴。我想尝试保持在当前标准范围内,以使我的代码更加可移植。有没有办法使用当前标准来处理这个问题,或者这是 BOOST_AUTO 能够处理的事情?我试图实现的一般功能是根据模板化参数的类型生成一个对象,而不实际将新对象的类型传递到函数中(即推断它)
谢谢
I have a templated function where the templated parameter is a functor. I want to generate some internal variables, specific to a particular quality function, to be used by other template functions called from this functor. so I have experimented with the auto keyword and it seems to work
template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
auto internal_variables = gen(quality_function);
float result = do_some_work(internal_variables);
...
return result;
}
struct internals_for_linearised_functor {
double x;
};
internals_for_linearised_functor gen(mynamespace::linearised_function &f) {
linearised_internals internals;
internals.x = 5;
return internals;
}
With auto this seems to compile fine, which is pleasing. I would like to try to stay within the current standard though to make my code more portable. Is there a way this can be handled using the current standard, or is this something that BOOST_AUTO will be able to handle? The general function I am trying to achieve is generate an object based on a on the type of templated paramater without actually passing the type of the new object into the function (i.e. inferring it)
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这应该是开箱即用的:
如果您需要
internal_variables
的存在时间比仅一次调用的时间长,您可以使用另一个小函数来推断类型:希望这会有所帮助!
This should work out of the box:
If you need
internal_variables
to be around longer than for just one call, you can use another small function to deduce the type:Hope this helps!
如果您不想在这种情况下使用
auto
,则需要使用某种元函数(可能是函数特征)来获取gen< 的返回类型/代码>。
Boost 具有函数特征,或者您可以专门为您的
QF
类型实现一个特征类。如果它是标准函数对象类型,它应该有一个result_type
typedef,您可以使用typedef typename QF::result_type result_type
获得它If you don't want to use
auto
in this case, you'll need to use some kind of meta-function (a function trait, perhaps) to get the return type ofgen
.Boost has function traits, or you could implement a traits class specifically for your
QF
type. If it's a standard function object type, it should have aresult_type
typedef, which you can get at usingtypedef typename QF::result_type result_type
您可以将
gen()
作为静态方法包装在结构中,然后替换internal_variables
吗?请参阅以下修改后的代码。现在,在函数
find_optimal_partition_louvain_with_gain
中,您可以首先调用Generate::gen()
,然后使用Generate::s_Internals
代替内部变量
。这似乎是避免了解
auto
类型的更简单方法。此外,如果需要,您可以将
Generate
设置为template
。对于上面代码中的线程安全,我们也可以在没有
static
的情况下实现。Can you wrap
gen()
as static method inside a struct and then replace theinternal_variables
? See the following modified code.Now, in function
find_optimal_partition_louvain_with_gain
, you can first callGenerate::gen()
and then use,Generate::s_Internals
instead ofinternal_variables
.This seems to be easier way to avoid knowing the type of
auto
.Also, you can make
Generate
astemplate<>
if needed.For thread safety in above code, we can implement without
static
also.