这是重载静态成员函数(提供与非静态成员函数相同的接口)的一种优雅方法吗? [英] Is this an elegant way to overload a static member function that provides the same interface as a non static member function?

查看:126
本文介绍了这是重载静态成员函数(提供与非静态成员函数相同的接口)的一种优雅方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下具有模板变量并使用模板别名和自动类型推断的非类模板。

Consider the following non class template that has a template variable and uses template aliasing and auto type deduction.

template<typename T>
using Type = T;

using TypeA = Type<int>;
using TypeB = Type<double>;

class Foo {
private:
    template<typename T>
    static Type<T> type_;
public:
    template<typename T>
    explicit Foo( Type<T> type ) { type_<T> = type; }

    // non static member
    template<typename T>
    auto bar() { return type_<T>; }

    // static member
    template<typename T>
    static auto bar(T _x_ = 0) { return type_<T>; }
};

使用该程序的程序:

// has to be defined in some cpp file.
template<typename T>
Type<T> Foo::type_;

int main() {
     TypeA a{ 7 };
     TypeB b{ 3.41 };

     Foo f1( a );
     Foo f2( b );

     auto x = Foo::bar<TypeA>();
     auto y = Foo::bar<TypeB>();

     std::cout << "static auto f1: " << x << '\n';
     std::cout << "static auto f2: " << y << '\n';

     std::cout << "member f1: " << f1.bar<TypeA>() << '\n';
     std::cout << "member f2: " << f2.bar<TypeB>() << '\n';

     return 0;
};

输出

static auto f1: 7
static auto f2: 3.41
member f1: 7
member f2: 3.41

在类声明中;我在静态版本中使用参数 T 并将其默认设置为0,以便可以在不使用任何参数的情况下调用它。如果不添加此参数,则将无法重载没有参数或参数列表具有相同参数的静态和非静态成员函数。

In the class declaration; I'm using a parameter T in the static version and defaulting it to 0 so that this can be called without any parameters. If this is not added then one would not be able to overload a static and a non static member function that has no arguments or the same arguments for its parameter list.

应该这被认为是一种快速修复或黑客手段,或者这是一种能够为具有相同名称和功能的静态和非静态成员函数提供相同类型的接口的可行方法?

Would this be considered a quick fix or a hack, or is this a possible way to be able to provide the same type of interface for both a static and non static member function with the same name and functionality?

函数参数或参数是伪参数,对内部值绝对没有作用。

The function argument or parameter is a dummy argument as does absolutely nothing to the internal value.

回到并再次阅读此书后,我可以看到一些混乱的起因,而我忽略了这样的事实,即这更多地与变量模板的使用有关,能够访问它们。

After coming back and reading this again I can see were some of the confusion was coming from and I left out the facts that this pertains more to the use of variable templates and being able to access them.

因此,我认为真正的问题应该是:关于变量模板成员并且要求它们是静态的,首选的方法是通过成员函数访问它们:通过静态或非静态,还是没有偏好,而选择权则由程序员决定?

So I think the real question should of been: Concerning variable template members and that they are required to be static, what is the preferred way to access them through a member function: via static or non static, or is there no preference and that choice is left up to the programmer?

另一件事;

推荐答案


这被认为是快速修复还是黑客入侵,或者这是能够为相同名称的静态和非静态成员函数提供相同类型接口的正确方法?

Is this considered a quick fix or a hack, or is this the proper way to be able to provide the same type of interface for both a static and non static member function of the same name?

没有适当的方法来实现不正确的目标。除此之外,您的版本没有提供相同的界面。非静态版本需要一个显式的模板参数,而静态版本可以使用推导,如 f1.bar(1)一样。我不确定在这种情况下我是否建议使用推论(因为代码更加含糊),但是可以提供。

There is no proper way to accomplish an improper goal. Besides which, your version does not provide the same interface. The non-static version requires an explicit template argument, whereas the static version can use deduction, as in f1.bar(1). I'm not sure I'd recommend using the deduction in this case (because the code is more cryptic), but the possibility is provided. Your improper goal is not even met.

如果功能具有相同的功能(如您的示例),那么非静态版本将毫无意义。仅提供静态版本,如果有人想从对象中调用它,那就可以了。

If the functions have the same functionally (as in your example), then the non-static version is pointless overhead. Provide just the static version, and if someone wants to call it from an object, that's A-OK.

如果函数的功能不相同(也许是您的示例)过于简化了?),然后给他们起同样的名字是个坏主意。这包括非静态版本可以根据 * this 返回不同值的情况。至少在这种情况下,应该将静态版本重命名为 bar_no_object()之类的名称,以将其与依赖于对象的版本区分开。

If the functions do not have the same functionally (maybe your example was oversimplified?), then giving them the same name is a Bad Idea. This includes the case where the non-static version can return different values depending on *this. At the very least in this case, the static version should be renamed to something like bar_no_object() to distinguish it from the version that depends on the object.


另一件事;

One other last thing; are there any unforeseen issues that may lead to future consequences with this type of design pattern - interface?

嗯,基本上,您是在设置自己吗?充满混乱。其他人都希望 Foo :: bar() Foo {}。bar()调用相同的内容(静态)功能,而您正试图打破它。

Well, basically you are setting yourself up for a load of confusion. Everyone else will expect Foo::bar() and Foo{}.bar() to invoke the same (static) function, and you are trying to break that.

请注意编译器的消息。对其他所有人的期望是您无法重载没有参数或参数列表具有相同参数的静态和非静态成员函数的原因。您试图造成歧义,编译器阻止了您。有充分的理由。在询问变通办法是否有效之前,也许您应该问过编译器停止了您的原因?

Pay attention to your compiler's messages. This expectation of everyone else is the reason you were not able "to overload a static and a non static member function that has no arguments or the same arguments for its parameter list." You tried to create ambiguity and the compiler stopped you. With good reason. Before asking if your workaround is valid, perhaps you should have asked for the reason the compiler stopped you?

这篇关于这是重载静态成员函数(提供与非静态成员函数相同的接口)的一种优雅方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆