[英]How to implement this type specific object generator without using C++0x 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
所以我尝试了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;
}
With auto this seems to compile fine, which is pleasing. 使用auto时,这似乎可以编译良好,这很令人满意。 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?
有没有一种方法可以使用当前标准进行处理,或者BOOST_AUTO将能够处理此问题? 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 (ie inferring it)
我要实现的一般功能是基于模板化参数的类型生成一个对象,而没有实际将新对象的类型传递给该函数(即推断它)
Thanks 谢谢
This should work out of the box: 这应该开箱即用:
template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
return do_some_work(gen(quality_function));
}
If you need internal_variables
to be around longer than for just one call, you can use another small function to deduce the type: 如果您需要
internal_variables
长度比一次调用的长度更长,则可以使用另一个小函数来推断类型:
template<typename QF, typename T>
float find_optimal_partition_louvain_with_gain_detail(QF quality_function, T internal) {
float result = do_some_work(internal);
/* use internal and quality_function again here!.*/
return result;
}
template<typename QF>
float find_optimal_partition_louvain_with_gain(QF quality_function) {
return find_optimal_partition_louvain_with_gain_detail(
quality_function,
gen(quality_function)
);
}
Hope this helps! 希望这可以帮助!
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 of gen
. 如果您不想在这种情况下使用
auto
,则需要使用某种元函数(也许是函数特性)来获取gen
的返回类型。
Boost has function traits, or you could implement a traits class specifically for your QF
type. Boost具有功能特征,或者您可以实现专门针对您的
QF
类型的特征类。 If it's a standard function object type, it should have a result_type
typedef, which you can get at using typedef typename QF::result_type result_type
如果它是标准函数对象类型,则应具有
result_type
typedef,您可以使用typedef typename QF::result_type result_type
Can you wrap gen()
as static method inside a struct and then replace the internal_variables
? 您可以将
gen()
作为静态方法包装在struct中,然后替换internal_variables
吗? See the following modified code. 请参阅以下修改的代码。
struct Generate
{
static internals_for_linearised_functor s_Internals; // define somewhere
static void gen(mynamespace::linearised_function &f)
{
linearised_internals internals;
internals.x = 5;
s_Internals = internals;
}
};
Now, in function find_optimal_partition_louvain_with_gain
, you can first call Generate::gen()
and then use, Generate::s_Internals
instead of internal_variables
. 现在,在函数
find_optimal_partition_louvain_with_gain
,您可以先调用Generate::gen()
,然后使用Generate::s_Internals
而不是internal_variables
。
This seems to be easier way to avoid knowing the type of auto
. 这似乎是避免知道
auto
类型的更简单的方法。 Also, you can make Generate
as template<>
if needed. 此外,您还可以使
Generate
的template<>
如果需要的话。 For thread safety in above code, we can implement without static
also. 为了上述代码中的线程安全 ,我们也可以不使用
static
来实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.