casting

Understanding gsl::narrow implementation

牧云@^-^@ 提交于 2019-12-17 20:23:02
问题 The C++ Core Guidelines has a narrow cast that throws if the cast changes the value. Looking at the microsoft implementation of the library: // narrow() : a checked version of narrow_cast() that throws if the cast changed the value template <class T, class U> T narrow(U u) noexcept(false) { T t = narrow_cast<T>(u); if (static_cast<U>(t) != u) gsl::details::throw_exception(narrowing_error()); if (!details::is_same_signedness<T, U>::value && ((t < T{}) != (u < U{}))) // <-- ??? gsl::details:

Struct pointer compatibility

我的未来我决定 提交于 2019-12-17 20:18:22
问题 Suppose we have two structs: typedef struct Struct1 { short a_short; int id; } Struct1; typedef struct Struct2 { short a_short; int id; short another_short; } Struct2; Is it safe to cast from Struct2 * to Struct1 * ? What does the ANSI spec says about this? I know that some compilers have the option to reorder structs fields to optimize memory usage, which might render the two structs incompatible. Is there any way to be sure this code will be valid, regardless of the compiler flag? Thank you

Most portable and reliable way to get the address of variable in C++

倖福魔咒の 提交于 2019-12-17 20:13:25
问题 Using & to get an address of a variable can be problematic if the variable type has overloaded operator&() . For example, _com_ptr_ has operator&() overloaded with a side effect of modifying the object. Now I have a complicated set of templates with functions like this: template<class T> void process( const T* object ) { //whatever } template<class T> void tryProcess( T& object ) { process( &object ) } In tryProcess() I need to get a T* pointer holding the address of the actual object of type

Should static_cast<Derived *>(Base pointer) give compile time error?

心已入冬 提交于 2019-12-17 18:52:28
问题 Should static_cast(Base pointer) give compile time error? class A { public: A() { } }; class B : public A { public: B() { } }; int main() { A *a=new A(); B * b=static_cast<B*>(a); // Compile Error? } 回答1: It cannot give compile time error because a Base-Derived relationship can exist at runtime depending on the address of the pointers being casted. static_cast always succeeds, but will raise undefined-behavior if you don't cast to the right type. dynamic_cast may fail or not, actually telling

Is it OK to use C-style cast for built-in types?

萝らか妹 提交于 2019-12-17 18:49:09
问题 After reading here a lot of answers about C-style casting in C++ I still have one little question. Can I use C-style casting for built-in types like long x=(long)y; or it's still considered bad and dangerous? 回答1: I would not, for the following reasons: Casts are ugly and should be ugly and stand out in your code, and be findable using grep and similar tools. "Always use C++ casts" is a simple rule that is much more likely to be remembered and followed than, "Use C++ casts on user-defined

Casting Class Pointer to Void Pointer

前提是你 提交于 2019-12-17 18:38:37
问题 How can I able to cast a class pointer to a generic pointer like void*? Like is this code valid?, class CFoo { int a; public: CFoo():a(1){} ~CFoo(){} getNum(){return a;} }; void tfunc(void* data) { CFoo* foo = static_cast<CFoo*>(data); std::cout << "Number: " << foo->getNum(); delete foo; } int main() { CFoo* foo = new CFoo; void* dt = static_cast<void*>(foo); tfunc(dt); // or tfunc(static_cast<void*>(food)); return 0; } 回答1: This is perfectly valid. Here is what standard has to say about it:

warning: assignment makes integer from pointer without a cast

荒凉一梦 提交于 2019-12-17 18:34:14
问题 When I declare a char * to a fixed string and reuse the pointer to point to another string /* initial declaration */ char *src = "abcdefghijklmnop"; ..... /* I get the "warning: assignment makes integer from pointer without a cast" */ *src ="anotherstring"; I tried to recast the pointer but no success. 回答1: The expression *src refers to the first character in the string, not the whole string. To reassign src to point to a different string tgt , use src = tgt; . 回答2: When you write the

Enum and performance

旧街凉风 提交于 2019-12-17 18:26:14
问题 My app has a lot of different lookup values, these values don't ever change, e.g. US States. Rather than putting them into database tables, I'd like to use enums. But, I do realize doing it this way involves having a few enums and a lot of casting from "int" and "string" to and from my enums. Alternative, I see someone mentioned using a Dictionary<> as a lookup tables, but enum implementation seems to be cleaner. So, I'd like to ask if keeping and passing around a lot of enums and casting

Type safety: Unchecked cast from Object

流过昼夜 提交于 2019-12-17 18:03:09
问题 I try to cast an object to my Action class, but it results in a warning: Type safety: Unchecked cast from Object to Action<ClientInterface> Action<ClientInterface> action = null; try { Object o = c.newInstance(); if (o instanceof Action<?>) { action = (Action<ClientInterface>) o; } else { // TODO 2 Auto-generated catch block throw new InstantiationException(); } [...] Thank you for any help 回答1: Yes - this is a natural consequence of type erasure. If o is actually an instance of Action<String

Proper way of casting pointer types

牧云@^-^@ 提交于 2019-12-17 17:39:15
问题 Considering the following code (and the fact that VirtualAlloc() returns a void*): BYTE* pbNext = reinterpret_cast<BYTE*>( VirtualAlloc(NULL, cbAlloc, MEM_COMMIT, PAGE_READWRITE)); why is reinterpret_cast chosen instead of static_cast ? I used to think that reinterpret_cast is OK for e.g. casting pointers to and from integer types (like e.g. DWORD_PTR ), but to cast from a void* to a BYTE* , isn't static_cast OK? Are there any (subtle?) differences in this particular case, or are they just