built-in-types

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

旧时模样 提交于 2019-12-01 00:18:06
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 intialized to the same value each time the program runs - the same stuff as with a constructor initializer

How are built-in types protected from overwriting (assigning to) their methods?

拜拜、爱过 提交于 2019-11-30 15:57:50
问题 I noted that int.__str__ = lambda x: pass yields an error. I can see, why that is forbidden. But how? Can I use that in "normal" code? 回答1: For setting attributes directly on int itself and other built-in types (rather than their instances), this protection happens in type.__setattr__, which specifically prohibits setting attributes on built-in types: static int type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) { int res; if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { PyErr

How are built-in types protected from overwriting (assigning to) their methods?

大兔子大兔子 提交于 2019-11-30 15:29:55
I noted that int.__str__ = lambda x: pass yields an error. I can see, why that is forbidden. But how? Can I use that in "normal" code? For setting attributes directly on int itself and other built-in types (rather than their instances), this protection happens in type.__setattr__ , which specifically prohibits setting attributes on built-in types: static int type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) { int res; if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { PyErr_Format( PyExc_TypeError, "can't set attributes of built-in/extension type '%s'", type->tp_name); return -1;

what happens when i mix signed and unsigned types ?

為{幸葍}努か 提交于 2019-11-28 23:20:35
I'm studying the C++ language and i have some doubt about type conversion, could you explain me what happens in an expression like this : unsigned int u = 10; int a = -42; std::cout << u - a << std::endl; Here i know that the result will be 52 if i apply the rules when we have two mathematical operators.But i wonder what happens when the compiler to convert a to an unsigned value creates a temporary of unsigned type, what happens after ? The expression now should be 10 -4294967254. In simple terms, if you mix types of the same rank (in the sequence of int , long int , long long int ), the

Do built-in types have move semantics?

家住魔仙堡 提交于 2019-11-27 11:49:14
Consider this code: #include <iostream> using namespace std; void Func(int&& i) { ++i; } int main() { int num = 1234; cout << "Before: " << num << endl; Func(std::move(num)); cout << "After: " << num << endl; } Its output is: Before: 1234 After: 1235 Clearly, i is being modified inside Func , as it is bound to parameter i after being "converted" to an r-value reference by std::move . Well, my point: Moving an object means transferring ownership of resources from one object into another. However, built-in types holds no resources because they themselves are the resources. It makes no sense to

Is there a function to check if an object is a builtin data type?

跟風遠走 提交于 2019-11-27 06:21:09
问题 I would like to see if an object is a builtin data type in C# I don't want to check against all of them if possible. That is, I don't want to do this: Object foo = 3; Type type_of_foo = foo.GetType(); if (type_of_foo == typeof(string)) { ... } else if (type_of_foo == typeof(int)) { ... } ... Update I'm trying to recursively create a PropertyDescriptorCollection where the PropertyDescriptor types might not be builtin values. So I wanted to do something like this (note: this doesn't work yet,

Constructor to specify zero-initialization of all builtin members?

不问归期 提交于 2019-11-27 04:40:21
问题 Is there a simpler way for a class's constructor to specify that all members of built-in type should be zero-initialized? This code snippet came up in another post: struct Money { double amountP, amountG, totalChange; int twenty, ten, five, one, change; int quarter, dime, nickel, penny; void foo(); Money() {} }; and it turned out that the problem was that the object was instantiated via Money mc; and the variables were uninitialized. The recommended solution was to add the following

What is the difference between isinstance('aaa', basestring) and isinstance('aaa', str)?

帅比萌擦擦* 提交于 2019-11-27 02:35:18
a='aaaa' print isinstance(a, basestring)#true print isinstance(a, str)#true In Python versions prior to 3.0 there are two kinds of strings "plain strings" and "unicode strings". Plain strings ( str ) cannot represent characters outside of the Latin alphabet (ignoring details of code pages for simplicity). Unicode strings ( unicode ) can represent characters from any alphabet including some fictional ones like Klingon. So why have two kinds of strings, would it not be better to just have Unicode since that would cover all the cases? Well it is better to have only Unicode but Python was created

what happens when i mix signed and unsigned types ?

匆匆过客 提交于 2019-11-26 16:16:01
问题 I'm studying the C++ language and i have some doubt about type conversion, could you explain me what happens in an expression like this : unsigned int u = 10; int a = -42; std::cout << u - a << std::endl; Here i know that the result will be 52 if i apply the rules when we have two mathematical operators.But i wonder what happens when the compiler to convert a to an unsigned value creates a temporary of unsigned type, what happens after ? The expression now should be 10 -4294967254. 回答1: In

Do built-in types have move semantics?

帅比萌擦擦* 提交于 2019-11-26 15:46:37
问题 Consider this code: #include <iostream> using namespace std; void Func(int&& i) { ++i; } int main() { int num = 1234; cout << "Before: " << num << endl; Func(std::move(num)); cout << "After: " << num << endl; } Its output is: Before: 1234 After: 1235 Clearly, i is being modified inside Func , as it is bound to parameter i after being "converted" to an r-value reference by std::move . Well, my point: Moving an object means transferring ownership of resources from one object into another.