简体   繁体   English

已具有返回类型的函数声明中的模板声明?

[英]Template declaration in function declaration that already has a return type?

So I'm following a C++/SDL2 programming tutorial series for 2D gaming.所以我正在关注 2D 游戏的 C++/SDL2 编程教程系列。 I was following along on this video and he wrote this bit of code.我一直在关注这个视频,他写了这段代码。

using ComponentID = std::size_t;

inline ComponentID getComponentID()
{
    static ComponentID lastID = 0;
    return lastID++;
}

template <typename T> inline ComponentID getComponentID() noexcept
{
    static ComponentID typeID = getComponentID();
    //typeID below is being used like a function???
    return typeID(); //<- error and won't compile, I believe this was a typo
}

I don't understand why template<typename T> is put on that line for inline ComponentID getComponentID() noexcept .我不明白为什么template<typename T>被放在那一行用于inline ComponentID getComponentID() noexcept Why is that there?为什么会在那里? Obviously, there's no semicolon after a template declaration so I suppose it can be put on the same line but why?显然,模板声明后没有分号,所以我想它可以放在同一行,但为什么呢? Is there something I'm missing?有什么我想念的吗? I haven't finished the video yet as this perplexed me and didn't wanna just go through the whole video copying code and not understanding it.我还没有完成视频,因为这让我感到困惑,不想只是浏览整个视频复制代码而不理解它。 Any hints?任何提示?

EDIT: I know how templates work, I don't need a lesson on that.编辑:我知道模板是如何工作的,我不需要这方面的课程。 But there's already a return type in the declaration of that function which is what I was asking about.但是在该函数的声明中已经有一个返回类型,这就是我要问的。 Not for strangers to teach me about the C++ language.不是让陌生人教我 C++ 语言。

template <typename T> inline ComponentID getComponentID() noexcept

ComponentID aka std::size_t already specifies the return type does it not? ComponentID aka std::size_t已经指定了返回类型不是吗? T 's definition is put on the same line and is not used in that function. T的定义放在同一行上,不在该函数中使用。 This is what I'm asking about.这就是我要问的。 Did I miss something?我错过了什么? Because I didn't think you could have multiple return types for a function.因为我不认为一个函数可以有多种返回类型。 So since it isn't the return type and it isn't used in the function itself or as a type for a parameter what is its use in this context?因此,既然它不是返回类型,也没有在函数本身中使用,也没有用作参数的类型,那么它在此上下文中的用途是什么?

The goal is to have unique incremental ID by type.目标是按类型拥有唯一的增量 ID。

So with所以与

ComponentID getComponentID()
{
    static ComponentID lastID = 0;
    return lastID++;
}

template <typename T>
ComponentID getComponentID() noexcept
{
    static ComponentID typeID = getComponentID();

    return typeID; // () was a typo indeed
}

We will have我们将有

struct Component1 {};
struct Component2 {};


getComponent<Component1>(); // Will return 0
getComponent<Component2>(); // will return 1
getComponent<Component1>(); // Will return 0
getComponent<Component2>(); // will return 1

Templates are used in order to adapt the function or a class to support more than one build in or custom type.模板用于调整函数或类以支持多个内置或自定义类型。 by doing so you can have different types used in the same code, ultimately reducing the duplicate code.通过这样做,您可以在同一代码中使用不同的类型,最终减少重复代码。

imagine that your getComponentID() had to return an int and also in another scenario need it to return a double.想象一下,您的 getComponentID() 必须返回一个 int 并且在另一种情况下需要它返回一个 double。 if getcomponentid() wasn't a template function, you had to overload the function to handle both int and double return types.如果getcomponentid()不是模板函数,则必须重载该函数以处理 int 和 double 返回类型。 Since you are using templates you don't need to deal with that.由于您使用的是模板,因此您无需处理它。 the compiler will handle it for you.编译器会为你处理。

one thing to note though, templates can slow down your compilation time.但是需要注意的一件事是,模板会减慢您的编译时间。

http://www.cplusplus.com/doc/oldtutorial/templates/ http://www.cplusplus.com/doc/oldtutorial/templates/

Following up on your edit:跟进您的编辑:

he is not returning a template type.他没有返回模板类型。 instead of doing;而不是做;

template<typename T>
inline ComponentID getComponentID()
{
...
...
}

he is putting everything in one line.as follows.他将所有内容都放在一行中。如下。

template <typename T> inline ComponentID getComponentID()

yours truly, some stranger from the net -.-你真的,来自网络的一些陌生人-.-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM