built-in-types

Destructor call for scalar types & undefined behavior [duplicate]

故事扮演 提交于 2021-02-08 02:58:34
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

Destructor call for scalar types & undefined behavior [duplicate]

走远了吗. 提交于 2021-02-08 02:58:09
问题 This question already has an answer here : Pseudo-destructor call does not destroy an object (1 answer) Closed 5 years ago . I just wrote following program & it compiles & runs fine. (see live demo here.) #include <iostream> typedef int T; int main() { int a=3; std::cout<<a<<'\n'; a.~T(); std::cout<<a; return 0; } Why the program compiles fine? If I am not wrong scalar types don't have constructor and destructor in C++ . So, is this program well defined? Does explicit call to destructor

How do I value-initialize a Type* pointer using Type()-like syntax?

早过忘川 提交于 2020-01-23 04:31:08
问题 Variables of built-in types can be value-initialized like this: int var = int(); this way I get the default value of int without hardcoding the zero in my code. However if I try to do similar stuff for a pointer: int* ptr = int*(); the compiler (Visual C++ 10) refuses to compile that (says type int unexpected ). How do I value-initialize a pointer in similar manner? 回答1: How do I value-initialize a Type* pointer using Type()-like syntax? You cannot. The syntax T() is defined in 5.2.3/1,2 (C+

Redefining Pythons builtin datatypes

寵の児 提交于 2020-01-01 14:43:49
问题 Is it possible to redefine which object the brackets [] use? I can subclass the list object, but how to I make the interpreter use my subclass in place of the buildin list object? Is it possible? (I'm pretty sure I'm using the wrong terms for the question- feel free to edit) >>> class mlist(list): ... def __init__(self): ... list.__init__(self) ... def __getitem__(self, item): ... return list.__getitem__(self, item) * 2 ... >>> testlist = mlist() >>> testlist.append(21) >>> testlist[0] 42 >>>

How is “int* ptr = int()” value initialization not illegal?

独自空忆成欢 提交于 2019-12-20 08:32:17
问题 The following code (taken from here): int* ptr = int(); compiles in Visual C++ and value-initializes the pointer. How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer. How is the code above not illegal? 回答1: int() is a constant expression with a value of 0, so it's a valid way of producing a null pointer constant. Ultimately, it's just a slightly different way of saying int *ptr = NULL; 回答2: Because int() yields 0 , which is interchangeable

How is “int* ptr = int()” value initialization not illegal?

点点圈 提交于 2019-12-20 08:32:10
问题 The following code (taken from here): int* ptr = int(); compiles in Visual C++ and value-initializes the pointer. How is that possible? I mean int() yields an object of type int and I can't assign an int to a pointer. How is the code above not illegal? 回答1: int() is a constant expression with a value of 0, so it's a valid way of producing a null pointer constant. Ultimately, it's just a slightly different way of saying int *ptr = NULL; 回答2: Because int() yields 0 , which is interchangeable

Python Division Of Complex Numbers Without Using Built In Types and Operators

前提是你 提交于 2019-12-14 01:17:53
问题 I have to implement a class called ComplexNumbers which is representing a complex number and I'm not allowed to use the built in types for that. I already have overwritten the operators ( __add__ , __sub__ , __mul__ , __abs__ , __str_ which allows to perform basic operations. But now I'm stuck with overwriting the __div__ operator. Allowed to use: I'm using float to represent the imaginary part of the number and float to represent the rel part. What I have already tried: I looked up how to

How to default-initialize local variables of built-in types in C++?

牧云@^-^@ 提交于 2019-12-12 07:20:47
问题 How do I default-initialize a local variable of primitive type in C++? For example if a have a typedef: typedef unsigned char boolean;//that's Microsoft RPC runtime typedef I'd like to change the following line: boolean variable = 0; //initialize to some value to ensure reproduceable behavior retrieveValue( &variable ); // do actual job into something that would automagically default-initialize the variable - I don't need to assign a specific value to it, but instead I only need it to be

Default construction of elements in a vector

廉价感情. 提交于 2019-12-10 13:32:43
问题 While reading the answers to this question I got a doubt regarding the default construction of the objects in the vector. To test it I wrote the following test code: struct Test { int m_n; Test(); Test(const Test& t); Test& operator=(const Test& t); }; Test::Test() : m_n(0) { } Test::Test(const Test& t) { m_n = t.m_n; } Test& Test::operator =(const Test& t) { m_n = t.m_n; return *this; } int main(int argc,char *argv[]) { std::vector<Test> a(10); for(int i = 0; i < a.size(); ++i) { cout<<a[i]

int a=int(); what happens in C++98?

微笑、不失礼 提交于 2019-12-09 16:30:36
问题 Please read the question entirely before you think to mark it as duplicate. The statement like int i=int(); most programmers will say that there is value initialization here & i will be value initialized. (0 as output). But it also prints 0 as output on C++98 compiler. Following program that I tested on C++98 implementation and gives me 0 as output. #include <iostream> int main() { int i=int(); std::cout<<i; } Don't say that i is value initialized in above C++98 program ,because value