implicit-conversion

How to implicitly wrap a value that can be null or an array into an Scala Option

元气小坏坏 提交于 2019-12-22 09:57:07
问题 I have this Java class in a Jar file included as a dependency of an Scala program (like the Axis jar): class MyClass { private String[] someStrings; public String[] getSomeStrings() { return someStrings; } } In my Scala program I have a Java API that return an instance of MyClass instance of MyClass to my program in Scala: val myInstance = JavaAPI.getInstanceOfMyClass() Then I try to use the someStrings array in my Scala program but it's null (let say that it wasn't properly initialized) for

Function member pointer with private base

北战南征 提交于 2019-12-22 07:04:10
问题 The following code yields a compile time error: ' base::print ' : cannot access private member declared in class ' base_der ' However, I have made the member public in the derived class. Why doesn't this work? #include <iostream> using namespace std; class base { public: int i; void print(int i) { printf("base i\n"); } }; class base_der : private base { public: using base::print; }; int main() { // This works: base_der cls; cls.print(10); // This doesn't: void (base_der::* print)(int); print

Why does this implicit type conversion in C# fail?

▼魔方 西西 提交于 2019-12-22 06:42:46
问题 Background: Let's assume I've got the following class: class Wrapped<T> : IDisposable { public Wrapped(T obj) { /* ... */ } public static implicit operator Wrapped<T>(T obj) { return new Wrapped<T>(obj); } public void Dispose() { /* ... */ } } As you can see, it provides an implicit type conversion operator for T → Wrapped<T> . Ultimately, I would like to be able to use this class as follows: interface IX { /* ... */ } class X : IX { /* ... */ } ... IX plainIX = new X(); using (Wrapped<IX>

C# why must conversion operator must be declared static and public?

一世执手 提交于 2019-12-22 05:30:10
问题 Regarding the following compiler error: User-defined operator 'Foo.implicit operator Foo(Bar)' must be declared static and public What is the reason for this? Why must a user-defined conversion operator be public ? Give the following code, why wouldn't this conversion be legal: internal class Bar{} internal class Foo { private Bar _myBar; internal static implicit operator Bar(Foo foo) { return foo._myBar; } } I checked the C# Language Spec and the relevant section (10.10.3) didn't mention

Implicit conversion with null-coalescing operator

你说的曾经没有我的故事 提交于 2019-12-22 05:11:18
问题 I discovered a strange behaviour of my program and after futher analysis, I was able to find that there is probably something wrong either in my C# knowledge or somewhere else. I beleive it's my mistake but I cannot find an answer anywhere... public class B { public static implicit operator B(A values) { return null; } } public class A { } public class Program { static void Main(string[] args) { A a = new A(); B b = a ?? new B(); //b = null ... is it wrong that I expect b to be B() ? } } The

Implicitly convert method group to Delegate (for argument of Control.Invoke)

拟墨画扇 提交于 2019-12-22 04:30:14
问题 I'm working on a Windows Forms application, and it contains custom controls with methods that can potentially be called from threads other than the UI thread. So these methods therefore look a bit like this to prevent exceptions: public void DoSomeStuff() { if (InvokeRequired) { Invoke((Action)DoSomeStuff); } else { // Actually do some stuff. } } The explicit cast of the method group DoSomeStuff to an Action caught my attention, and so I've been looking into delegates and other related

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

老子叫甜甜 提交于 2019-12-22 04:03:48
问题 The Standard Library class template std::bitset<N> has a constructor (C++11 and onwards, unsigned long argument before C++11) constexpr bitset(unsigned long long) noexcept Contrary to many best-practice guidelines, this single-argument constructor is not marked as explicit . What is the rationale behind this? 回答1: Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64; std::bitset<N> b

Why is the std::bitset constructor with an unsigned long long argument not marked as explicit?

假装没事ソ 提交于 2019-12-22 04:03:11
问题 The Standard Library class template std::bitset<N> has a constructor (C++11 and onwards, unsigned long argument before C++11) constexpr bitset(unsigned long long) noexcept Contrary to many best-practice guidelines, this single-argument constructor is not marked as explicit . What is the rationale behind this? 回答1: Explicit construction The main objection against an explicit constructor is that copy-initialization from unsigned integers no longer works constexpr auto N = 64; std::bitset<N> b

Scala StringBuilder

烂漫一生 提交于 2019-12-22 03:54:09
问题 Is there an implicit method to convert scala.collection.mutable.StringBuilder to java.lang.StringBuilder? I am using a Java library (JCommander) in which one of the methods ( usage ) takes a java.jang.StringBuilder argument. 回答1: You can't start with a Scala StringBuilder and then obtain the Java version. You can, however, wrap a java.lang.StringBuilder in the Scala version. So: val jsb = new java.lang.StringBuilder(); val sb = new StringBuilder(jsb); // Do Scala-y stuff with sb JCommander

Why std::function does not implicitly convert to bool in C++11? [duplicate]

喜夏-厌秋 提交于 2019-12-21 20:39:57
问题 This question already has an answer here : No viable conversion from std::function to bool (1 answer) Closed 3 years ago . Consider the following code. #include <functional> int main(void) { std::function<void()> f1; if (f1) { /* ok */ ... } bool b = f1; /* compile-error */ bool B = !f1; /* ok */ ... } std::function<> converts implicitly to bool in some circumstances but not in all of them. Assigning it to a bool -variable does not work, whereas the result of an operation or using it in an if