.NET: Unable to cast object to interface it implements

I hat the same problems with a library of mine providing “plugin”-functionality… I got it finally working… Here was my problem: I had one main assembly using plugins, one assembly with the plugin (Plugin.dll) AND (important) another assembly providing the plugin-functionality (Library.dll). The Plugin.dll referenced the main assembly (in order to be able to extend … Read more

What exactly is or was the purpose of C++ function-style casts?

Function style casts bring consistency to primitive and user defined types. This is very useful when defining templates. For example, take this very silly example: template<typename T, typename U> T silly_cast(U const &u) { return T(u); } My silly_cast function will work for primitive types, because it’s a function-style cast. It will also work for … Read more

casting via void* instead of using reinterpret_cast [duplicate]

For types for which such cast is permitted (e.g. if T1 is a POD-type and T2 is unsigned char), the approach with static_cast is well-defined by the Standard. On the other hand, reinterpret_cast is entirely implementation-defined – the only guarantee that you get for it is that you can cast a pointer type to any … Read more

casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

For me (using Java 1.6.0_26), the first snippet gives the same exception as the second one. The reason is that the Arrays.asList(..) method does only return a List, not necessarily an ArrayList. Because you don’t really know what kind (or implementation of) of List that method returns, your cast to ArrayList<String> is not safe. The … Read more

What is the modern, correct way to do type punning in C++?

First of all, you assume that sizeof(long) == sizeof(int) == sizeof(float). This is not always true, and totally unspecified (platform dependent). Actually, this is true on my Windows using clang-cl and wrong on my Linux using the same 64-bit machine. Different compilers on the same OS/machine can give different results. A static assert is at … Read more

Can a cast operator be explicit?

Yes and No. It depends on which version of C++, you’re using. C++98 and C++03 do not support explicit type conversion operators But C++11 does. Example, struct A { //implicit conversion to int operator int() { return 100; } //explicit conversion to std::string explicit operator std::string() { return “explicit”; } }; int main() { A … Read more