A a; A a(); A a(""); 没有严谨的态度,就做不好C++

来源:互联网 发布:淘宝店铺体检中心 编辑:程序博客网 时间:2024/04/30 00:47

现在时间是2016-11-8 17:31:24

今天为了取质数的性能分析而写了一个用来计量代码块执行时间的类:

#pragma once#include <chrono>#include <string>class CostTime{public:  CostTime(const std::string& msg = "");  ~CostTime();private:  std::chrono::steady_clock::time_point t_;  std::string msg_;};#define __COST_TIME__ CostTime(__FUNCTION__);
#include "CostTime.h"#include <iostream>CostTime::CostTime(const std::string& msg /* = "" */)  : t_(std::chrono::steady_clock::now())  , msg_(msg){}CostTime::~CostTime(){  std::cout << msg_     << " [cost time: "    << std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::steady_clock::now() - t_ ).count()    << "ms]"     << std::endl;}

然后在一个函数中测试时如下:

  {    std::cout << "begin..." << std::endl;    CostTime ct();    std::this_thread::sleep_for(2s);  }

然后就发现析构函数没有被调用,折腾半天发现下面几种情况都是正常的:
CostTime ct(“”);
CostTime ct{};
CostTime ct{“”};
CostTime ct;
我的意图就是在这初始化一个对象。
这下就可以肯定是ct();这种写法,肯定存在新的知识点。

折腾未果之后,上了irc,去了c++牛人群里,寻求帮助:

<vig> hi,i have a double: what's diff between "A a;" and "A a();"?  A<vig> A is a class<Moto-chan> Well...<Moto-chan> Oh, nevermind, you mean double question, not you have the double data type.<Moto-chan> Confusing...<Moto-chan> In the case of a class, there's nothing different between those two, they'll both call the default constructor.<Moto-chan> And they'll both have the same value.<velco> A a(); is fucntion declaration;<Moto-chan> If it were not a class and were a primitive though, then it'd be different, as one would be uninitialized, and the other would be zero initialized, respectively.<Moto-chan> And as velco is saying, depends on the context aswell, is this inside a function, or at namespace scope.<vig> sorry for my pool en. A is a class, <xaxxon> A a(); is a function declaration - a funciton named a taking no parameters and returning a value of type A<vig> if i use "A a()", then i find destructor never be called<xaxxon> [00:53]  <velco> A a(); is fucntion declaration;<vig> and if i use "A a("");" , then everything is ok<Moto-chan> { A a(); } class A {};<geordi> <no output><Moto-chan> He's obviously talking about calling a constructor...<xaxxon> its' a vexing parse, right?* Moto-chan wishes freenode could stop being pedantic for just one minute and attempt to help someone.<velco> if the goal is to instantiate an object, the syntax is "A a;". Or "A a{};"<Princess17b29a> { A a(); a.x; } class A { int x; };<geordi> error: request for member 'x' in 'a', which is of non-class type 'A()'<Princess17b29a> it is a function declaration<Moto-chan> Since when can you declare functions inside a function o.O<et> since C.<velco> since C<velco> heh<Moto-chan> Fuck that noise<TheSchaf> { A a((0)); a.x; } class A { int x; };<geordi> error: no matching function for call to 'A::A(int)'<vig> yes ,i want to init a object, then i find "A a;" or "A a{};" or A a("") or A a{""} is working, just A a(); is not<xaxxon> vig: because the last one declares a functio<xaxxon> n<xaxxon> A a; is what you want<xaxxon> instead of A a();<Moto-chan> That's some damn confusing syntax right there.<Moto-chan> And here I thought I had a grasp of the fucking thing >.<<xaxxon> Moto-chan, are you drunk?<xaxxon> https://en.wikipedia.org/wiki/Most_vexing_parse<Moto-chan> A() calls constructor, A a() function declaration, A a(arg) calls constructor...<Moto-chan> ┻━┻︵ \(°□°)/ ︵ ┻━┻<vig> i write "bool operator ()() = delete;" in class A<Moto-chan> xaxxon: If so, more than I should be for the amount I drank<xaxxon> Moto-chan, i find it hard to believe you haven't run into MVP before<Moto-chan> I don't believe I have, as when I began I wouldn't have noticed, and when I actually was into programming far enough to have noticed anything like this, C++11 was already a thing and I was using {} by default.<xaxxon> it's like every 100th question on stack overflow<xaxxon> 100th c++ question<Moto-chan> I avoid that place whenever possible :P Too much misinformation :P<Moto-chan> It's useful sometimes, but other times... Meh, I'd ask here first.<xaxxon> sure.   but I'd just suppose it owuld be frequent here too<Moto-chan> Or consult a book/reference.<Moto-chan> ┐( ゚ー゚)┌<Moto-chan> Doubt I'll forget it now though xD<xaxxon> anyhow, that's it's name.. Most Vexing Parse and it's the "if it can be a variable or a function, it's a function" part of the spec<Moto-chan> Heh, good old spec, C++, the language that doesn't know about a stack, except for when it does.<Moto-chan> All it has is "automatic storage duration" until it gets to "stack unwinding"<Moto-chan> hue<Moto-chan> Or so I believe anyway<vig> thank you everyone ,thank you moto-chan . i think your version is right and easy to explain the question <Moto-chan> Nope, I was incorrect in this case. Listen to the other guys, I'm a moron :P<vig> a function declare in a function. i never think about that . princess17 xaxxon , you are right. thank you. all of you <xaxxon> good luck vig

好吧 我捉急的英语太尴尬,不过最后还是解开了我的迷惑。
多谢这些大神,真的大神。

知道原因之后,我测试过,发现函数中声明一个函数也是ok的,只是见到的少,
最后,在测试中,我发现了一条警告, 而无知的我并没有注意警告了(刚工作的前几年,我连一个警告都不会放过,现在都是写一次代码,最后一次编译成功的心态,让我没有去注意这明显的警告)。

好吧 最后说一句,送给自己:做一个好的程序员

0 0
原创粉丝点击