命名空间中的参数函数(C++)

发布于 2025-02-02 07:08:55 字数 774 浏览 6 评论 0原文

我需要定义具有相同目标的3个函数,但其​​行为基于3组常数值略有变化;换句话说,我可以简单地编写一个在所有三种情况下以这些值作为输入来执行此操作的函数。但是,由于确实有很多常数(其中只有3个不同的集合),我肯定会避免发表如此长的声明。此外,我需要在其他文件中进行相关计算中的一组常数。

我正在考虑使用名称空间,但是找不到任何适合自己想要实现的东西的东西。只是为了使事情变得更容易理解,这是我想要的示例(但显然不会编译):

int parametric_function() {
    return a_constant + 1; //'a_constant' isn't defined yet
}

namespace first_behaviour {
    const int a_constant = 10;

    //make the function use the variable 'a_constant' defined here in some way
    int (*f)() = parametric_function;
}

namespace second_behaviour {
    const int a_constant = 20;

    //make the function use the variable 'a_constant' defined here in some way
    int (*f)() = parametric_function;
}

如您所见,我只需要写我的参数函数一次,我可以使用名称空间来获取正确的功能和相关的常数集。您对我可以尝试做什么有任何建议吗?

I need to define 3 functions that have the same goal, but whose behaviours changes slightly based on 3 sets of constant values; in other words, i could simply write a function that does that in all 3 cases by taking those values as inputs. But since there really many constants (and only 3 different sets of those) i'd definitely avoid such a long function declaration. Furthermore, i'll need those sets of constants in other files for related computations.

I was thinking about using namespaces, but i couldn't find anything that suited what i wanted to achieve. Just to make things more comprehensible, here is an example of what i'd desire (but obviously doesn't compile):

int parametric_function() {
    return a_constant + 1; //'a_constant' isn't defined yet
}

namespace first_behaviour {
    const int a_constant = 10;

    //make the function use the variable 'a_constant' defined here in some way
    int (*f)() = parametric_function;
}

namespace second_behaviour {
    const int a_constant = 20;

    //make the function use the variable 'a_constant' defined here in some way
    int (*f)() = parametric_function;
}

As you can see, i'd only need to write my parametric function once, and i can use the namespace to get the right function and the associated set of constants. Do you have any suggestions on what i could try doing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

假装不在乎 2025-02-09 07:08:55

如果您只有一个变量,则可以直接作为模板参数传递,如

但是,如果您有多个,则可以将它们包裹在结构中:

#include <iostream>

struct FirstBehavior
{
    static constexpr int a_constant = 10;
};
struct SecondBehavior
{
    static constexpr int a_constant = 10;
};

template <typename T>
int ParametricFunction()
{
    return T::a_constant + 1;
}

int main()
{
    std::cout << ParametricFunction<FirstBehavior>() << '\n'; // 1
}

If you have only one variable, you can pass it directly as a template parameter, as suggested in this answer.

But if you have more than one, you can wrap them in a struct:

#include <iostream>

struct FirstBehavior
{
    static constexpr int a_constant = 10;
};
struct SecondBehavior
{
    static constexpr int a_constant = 10;
};

template <typename T>
int ParametricFunction()
{
    return T::a_constant + 1;
}

int main()
{
    std::cout << ParametricFunction<FirstBehavior>() << '\n'; // 1
}
将军与妓 2025-02-09 07:08:55

在C ++中,您有模板:

template <int a_constant>
int parametric_function() {
    return a_constant + 1;
}

namespace first_behaviour {
    auto f = parametric_function<10>;
}

In c++ you have templates:

template <int a_constant>
int parametric_function() {
    return a_constant + 1;
}

namespace first_behaviour {
    auto f = parametric_function<10>;
}
一曲爱恨情仇 2025-02-09 07:08:55

使用HolyblackCat对结构和模板的建议,这将是一种方法。

结构只是包含变量的包装器。在此示例中,我使其成为一个状态变量(非const),对结构包装器静态。它的要求是通过parameteric_function的预期名称。

我认为使用示例使用非const变量可能更普遍适用于其他类型,例如std :: stringstd :: vector或您可能需要的任何内容。

extern int(*f)();只是为了警告编译器。

#include <iostream>

using std::cout;

template <typename T>
int parametric_function() {
    ++T::a_variable;
    return T::a_variable;
}

namespace first_behaviour {
struct VarHolder {
    static inline int a_variable = 10;
};
extern int (*f)();
int (*f)() = ¶metric_function<VarHolder>;
} // first_behaviour

namespace second_behaviour {
struct OtherVarHolder {
    static inline int a_variable = 20;
};
extern int (*f)();
int (*f)() = ¶metric_function<OtherVarHolder>;
} // second_behaviour

int main() {
    int x = first_behaviour::f();
    int y = second_behaviour::f();
    cout << x << " " << y << "\n";
}

Using HolyBlackCat's suggestion of a struct and a template, here would be one approach.

The struct is just a wrapper to hold the variable. In this example, I made it a stateful variable (non-const), static to the struct wrapper. It has the requirement to be the expected name by the parameteric_function.

I thought making the example use a non-const variable might be more generally applicable for other types, such as std::string or std::vector or whatever you may need.

The extern int (*f)(); was just to squelch a compiler warning.

#include <iostream>

using std::cout;

template <typename T>
int parametric_function() {
    ++T::a_variable;
    return T::a_variable;
}

namespace first_behaviour {
struct VarHolder {
    static inline int a_variable = 10;
};
extern int (*f)();
int (*f)() = ¶metric_function<VarHolder>;
} // first_behaviour

namespace second_behaviour {
struct OtherVarHolder {
    static inline int a_variable = 20;
};
extern int (*f)();
int (*f)() = ¶metric_function<OtherVarHolder>;
} // second_behaviour

int main() {
    int x = first_behaviour::f();
    int y = second_behaviour::f();
    cout << x << " " << y << "\n";
}
亣腦蒛氧 2025-02-09 07:08:55

您可能可以使用模板 s进行。您可以:

template <int CONST_VAL>
int par_func();

template<>
int par_func<10>(){ return 4; }

template<>
int par_func<20>(){ return 1; }

template<>
int par_func<30>(){ return 9; }

然后,如果需要,您可以将这些名称与其他功能相提并论,也可以像这样离开它们。这也确保只能使用专业。

您还可以做示例:

template <int CONST_VAL>
int par_func(){
    return CONST_VAL + 1;
}

然后,您可以将其放入实现文件中,并明显实例化仅使用您使用的文件,例如:

template int par_func<10>();

您可以以相同的方式使用命名空间模型,例如:

namespace func1 {
    int(* func)() = &par_func<10>;
}

namespace func2 {
    int(* func)() = &par_func<20>;
}

Possibly you could do with templates. You could:

template <int CONST_VAL>
int par_func();

template<>
int par_func<10>(){ return 4; }

template<>
int par_func<20>(){ return 1; }

template<>
int par_func<30>(){ return 9; }

You could then alias these names to some other function if you want, or you can leave them like this. This also ensures that only the specialisations can be used.

You can also do your example like:

template <int CONST_VAL>
int par_func(){
    return CONST_VAL + 1;
}

You can then put this in an implementation file and explicilty instantiate only the ones you use, like:

template int par_func<10>();

You can use this the same way with your namespace model like:

namespace func1 {
    int(* func)() = &par_func<10>;
}

namespace func2 {
    int(* func)() = &par_func<20>;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文