简体   繁体   English

Class 模板参数默认值

[英]Class template parameter default value

There is a class template definition for time_point template class from standard C++ library:标准 C++ 库中的time_point模板 class 有一个 class 模板定义:

template<
    class Clock,
    class Duration = typename Clock::duration
> class time_point;

Can someone please explain me the line:有人可以向我解释一下:

class Duration = typename Clock::duration

? ? I know that we are defining a default value for the second template parameter Duration but the thing i don't understand is the ::duration part after Clock type name.我知道我们正在为第二个模板参数Duration定义一个默认值,但我不明白的是Clock类型名称后面的::duration部分。 Should't it be just:不应该只是:

class Duration = typename Clock

? ?

This would use the type of the Clock :这将使用Clock的类型:

class Duration = typename Clock

However, the Duration is a type different from Clock .但是, Duration是与Clock不同的类型。 There is a named requirement Clock .有一个命名要求Clock Among other things it specifies that a Clock should have a duration member alias.除其他外,它指定 Clock 应具有duration成员别名。

A member alias is something like this:成员别名是这样的:

 struct my_clock {
      using duration = int;
 };

So when you instantiate a time_point<my_clock> , the second template parameter becomes my_clock::duration .因此,当您实例化time_point<my_clock>时,第二个模板参数变为my_clock::duration The keyword typename is needed because it is a dependent name.需要关键字typename ,因为它是从属名称。 You need to assure the compiler that Clock::duration really is a type.您需要向编译器保证Clock::duration确实是一种类型。

Marek has already explain almost everything in his answer, this one is to improve it.马雷克已经在他的回答中解释了几乎所有内容,这个是为了改进它。

typename is need to tell compiler "look, duration is nested in Clock and depend on it, you have to resolve Clock first and then deduce Clock::duration " typename需要告诉编译器“看,duration 嵌套在Clock中并依赖它,你必须先解析Clock然后推断Clock::duration

here there's a stupid example of a dependent type, is not meant to be used in production code.这里有一个依赖类型的愚蠢示例,并不打算在生产代码中使用。

template<typename T>
class MyBase
{
public:
   using UnderlyingType = T;
   // ...
   double foo() { return 5.5; }
};

class MyIntChild : public MyBase<int>{};

class MyDoubleChild : public MyBase<double>{};

template<typename T, typename D = typename T::UnderlyingType>
D makeFoo(T& t)
{
   return t.foo();
}

MyIntChild a;
makeFoo<MyIntChild>(); // return 5 because return type is int;

This is called dependent type.这称为依赖类型。

Note class can contain typedef ( using = ) inside.注意 class 可以在里面包含typedef ( using = )。 Clock::duration is just referring to this type definition which is expected inside type passed as first argument. Clock::duration只是指这种类型定义,它应该在作为第一个参数传递的类型内部。

Now typename is needed to explain to compiler that depending symbol must be a type (it could be constant).现在需要typename向编译器解释依赖符号必须是一个类型(它可以是常量)。

Take a look on some clock documentation it has typedef for duration:看看一些时钟文档,它有typedef持续时间:

std::chrono::system_clock - cppreference.com std::chrono::system_clock - cppreference.com

Member types会员类型

Member type会员类型 Definition定义
rep signed arithmetic type representing the number of ticks in the clock's duration有符号算术类型,表示时钟持续时间内的滴答数
period a std::ratio type representing the tick period of the clock, in seconds表示时钟滴答周期的std::ratio类型,以秒为单位
duration std::chrono::duration <rep, period>, capable of representing negative durations std::chrono::duration <rep, period>,能够表示负持续时间
time_point std::chrono::time_point <std::chrono::system_clock> std::chrono::time_point <std::chrono::system_clock>

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

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