简体   繁体   English

以类为类型名的模板变量

[英]Template variables with class as typename

I am learning C++14, in that come across this template variable feature and that interested me to dig more on this and i tried with multiple examples to understand the template variables.我正在学习 C++14,因为遇到了这个模板变量特性,这让我有兴趣对此进行更多的挖掘,我尝试用多个示例来理解模板变量。 Say,说,

template <typename T>
T var;

var<int>;
var<float>;

Above code worked and it looked straight forward to understand too.上面的代码有效,它也很容易理解。 But, when I tried to use class name in place of int or float as shown above, result in calling the temporary object creation for the class FOO and calling corresponding C'tor & dtor for the temp object.但是,当我尝试使用类名代替如上所示的 int 或 float 时,导致为类 FOO 调用临时对象创建并为临时对象调用相应的 C'tor & dtor。

var<FOO>;  //FOO is a class

I have written a sample test program and its output for your understanding.我已经编写了一个示例测试程序及其输出以供您理解。 My question is that,我的问题是,

  1. Why var creates temp object?为什么 var 创建临时对象?
  2. How template variable differ for primitive datatypes and user defined datatypes?原始数据类型和用户定义数据类型的模板变量有何不同?

If it is irrelevant or duplicate, please point me to the source for clear understanding.如果不相关或重复,请指出来源以便清楚理解。

Refer code below,参考下面的代码,

class B
{
  public:
  B()
  {
      std::cout<<"\nB ctor"<<std::endl;
  }
  B(const B& obj)
  {
      std::cout<<"B copy ctor"<<std::endl;
  }
  int operator()()
  {
      std::cout<<"operator() called"<<std::endl;
  }
   void f()  {
   //::A().print();   
  }
  ~B()
  {
      std::cout<<"\n~ for B()"<<std::endl;
  }
  
};

//Declaring template variable
template<typename T>
 T g ;

int main() {
    g<int> = 30;
    g<float> = 30.02f;
    g<B> = B{};
    std::cout<<"g value:"<<g<int><<std::endl;
    std::cout<<"g value:"<<g<float>;
}

Output:输出:

 B ctor g value:30 g value:30.02 ~ for B()

No temporary object is created by this simple program:这个简单的程序不会创建临时对象:

int main() {
  var<SomeClass>;
}

A temporary object is created here:这里创建了一个临时对象:

int main() {
  var<SomeClass> = SomeClass{};
}

but that is because we did it with SomeClass{} .但那是因为我们用SomeClass{}做到了。 We then assigned that to the var<SomeClass> non-temporary object (global in many of your examples).然后我们将它分配var<SomeClass>非临时对象(在您的许多示例中都是全局对象)。

The code that runs here is在这里运行的代码是

 SomeClass::SomeClass()
 SomeClass::SomeClass()
 SomeClass::operator=(SomeClass&&)
 SomeClass::~SomeClass()
 SomeClass::~SomeClass()

in that order.以该顺序。

#include <iostream>

struct noisy {
  noisy() { std::cout << __func__ << "()\n"; }
  ~noisy() { std::cout << __func__ << "()\n"; }
  noisy(noisy&&) { std::cout << __func__ << "(&&)\n"; }
  noisy(noisy const&) { std::cout << __func__ << "(c&)\n"; }
  void operator=(noisy&&) { std::cout << __func__ << "(&&)\n"; }
  void operator=(noisy const&) { std::cout << __func__ << "(c&)\n"; }
};

template<class T>
T var;

int main() {
    std::cout << "Start of main\n";
    {
        var<noisy> = noisy{};
        std::cout << "Body of main\n";
    }
    std::cout << "End of main\n";
}

live example .活生生的例子

Output:输出:

 noisy() Start of main noisy() operator=(&&) ~noisy() Body of main End of main ~noisy()

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

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