default-constructor

Uninitialized std::complex constructor when using 'new'

旧街凉风 提交于 2019-12-08 18:51:04
问题 In profiling my program I realized that 10% of the code is spent in a stupid std::complex<double>() constructor, using new std::complex<double>[size_of_array] . I have searched through the web and the default constructor for std::complex seems to take as real and imaginary parts the values double(). Since C++ does not initialize double numbers, I wonder why g++ bothers to initialize std::complex with zeros, and whether I could work around this through the whole program in some way (*) (*)

Double brace initialization

淺唱寂寞╮ 提交于 2019-12-08 15:26:47
问题 Which constructor should be called in the following code and why? struct S { int i; S() = default; S(void *) : i{1} { ; } }; S s{{}}; If I use clang (from trunk), then the second one is called. If the second constructor is commented out, then S{{}} is still valid expression, but (I believe) move-constructor from default-constructed instance of S{} is called in the case. Why conversion constructor has priority over the default one in the very first case? The intention of such a combination of

Why does the parameterless Guid constructor generate an empty GUID?

情到浓时终转凉″ 提交于 2019-12-08 14:40:23
问题 Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()? Is there a particular use for an empty Guid? 回答1: Why does the parameterless Guid constructor generate an empty GUID rather than default to a generated one as with Guid.NewGuid()? Short answer: Because the language/runtime didn't let the designer of the Guid type define a default constructor. It's not only conventional that the value of a "default-constructed"

Member of Union has User-Defined Constructor

落爺英雄遲暮 提交于 2019-12-07 17:11:30
For the following code: class Foo{ int foo; public: Foo() : foo(13) {} int getFoo() const { return foo; } }; union Bar{ Foo fBar; double dBar; }; I believe this is fully legal in C++. http://en.cppreference.com/w/cpp/language/union#Explanation says: If two union members are standard-layout types, it's well-defined to examine their common subsequence on any compiler And thus in gcc I can do this : Bar bar = { Foo() } When I try this in Visual Studio 2008 I get the error: error C2620: member Bar::fBar of union Bar has user-defined constructor or non-trivial default constructor Error C2620 states

Detect compiler generated default constructor using reflection in C#

*爱你&永不变心* 提交于 2019-12-07 07:37:02
问题 I'm targeting .NET 3.5 SP1 and I'm using CommentChecker to validate my XML documentation, everything works OK until I get to a class like this: /// <summary> /// documentation /// </summary> public sealed class MyClass { /// <summary> /// documentation /// </summary> public void Method() { } } In the example above, as I understand, the compiler generates a default constructor for my class. The problem with this is that CommentChecker generates warnings telling me that the constructor is

Understanding implicitly declared default costructor

本小妞迷上赌 提交于 2019-12-07 07:14:49
问题 I'm trying to understand how the compiler's default constructor works. I made this example: #include <iostream> class Base { public: int number; }; class Test1 : public Base { }; class Test2 { public: Base base; }; int main() { Test1 test1; Test2 test2; std::cout<<test1.number<<std::endl; std::cout<<test2.base.number<<std::endl; } The output of this test program is, for test1 0 , and for test2 is a uninitialized (random) number. Now my question is: why in the first case ( test1 ) the compiler

why default constructor is not present for a class containing const data members

无人久伴 提交于 2019-12-07 06:43:11
问题 why default constructor is not added by the compiler for the class containing constant data members. please see the below code , in that i have declared constant data member 'a' and while trying to create object for a class 'ClassA' it is saying No Appropriate Default constructor is available . please help. #include "stdafx.h" #include <iostream> using namespace std; class ClassA { private: const int a; public : void print() { cout << "hello world" << endl; } }; int main() { ClassA obj; obj

Should trivial default constructor respect default member initializer here?

邮差的信 提交于 2019-12-07 06:40:27
问题 Consider the code: #include <atomic> #include <iostream> struct stru { int a{}; int b{}; }; int main() { std::atomic<stru> as; auto s = as.load(); std::cout << s.a << ' ' << s.b << std::endl; } Note that although stru has default member initializer, it still qualifies as an aggregate type since C++14. std::atomic has a trivial default constructor. According to the standard, should the members of as be initialized to zero? clang 6.0.0 doesn't do this (see here), while gcc 7.2.0 seems so (see

Need an example showing that default constructor is not inherited

荒凉一梦 提交于 2019-12-07 04:59:14
问题 I know that default constructor is not inherited, as stated in n3337. And there is an example there: struct B2 { B2(int = 13, int = 42); }; struct D2 : B2 { using B2::B2; }; With quite good explanation: The candidate set of inherited constructors in D2 for B2 is ... —B2(int = 13, int = 42) —B2(int = 13) —B2() And most important: The set of constructors present in D2 is —D2() , implicitly-declared default constructor, not inherited For me this example does not show the difference, in a sense

Google Mock: “no appropriate default constructor available”?

吃可爱长大的小学妹 提交于 2019-12-07 00:14:00
问题 Using Visual Studio 2010 C++ with googlemock. I'm trying to use a mock I created and I'm getting the compiler error on the line: EmployeeFake employeeStub; The error is: 1>c:\someclasstests.cpp(22): error C2512: 'MyNamespace::EmployeeFake' : no appropriate default constructor available EmployeeFake: class EmployeeFake: public Employee{ public: MOCK_CONST_METHOD0(GetSalary, double()); } Employee: class Employee { public: Employee(PensionPlan *pensionPlan, const char * fullName); virtual