default-implementation

How do I provide a default implementation for an Objective-C protocol?

笑着哭i 提交于 2021-02-05 20:21:44
问题 I'd like to specify an Objective-C protocol with an optional routine. When the routine is not implemented by a class conforming to the protocol I'd like to use a default implementation in its place. Is there a place in the protocol itself where I can define this default implementation? If not, what is the best practice to reduce copying and pasting this default implementation all over the place? 回答1: Objective-C protocols have no affordance for default implementations. They are purely

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

与世无争的帅哥 提交于 2020-05-28 13:48:02
问题 Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

Is it safe to place definition of specialization of template member function (withOUT default body) in source file?

好久不见. 提交于 2020-05-28 13:47:59
问题 Here's what I mean: // test.h class cls { public: template< typename T > void f( T t ); }; - // test.cpp template<> void cls::f( const char* ) { } - // main.cpp int main() { cls c; double x = .0; c.f( x ); // gives EXPECTED undefined reference (linker error) const char* asd = "ads"; c.f( asd ); // works as expected, NO errors return 0; } This is completely fine, right? I started doubting this, because I just ran over the specialization of '...' after instantiation error, which was new to me.

How can you provide default implementations for UIPageViewControllerDataSource?

前提是你 提交于 2019-12-19 04:09:44
问题 I assume that the answer to this question will address issues with Objective-C protocols in general, but this is the first problem of this type that I've come across. I expect for these methods to be used, when implementing UIPageViewControllerDataSourceWithConnections . import UIKit protocol UIPageViewControllerDataSourceWithConnections: UIPageViewControllerDataSource { var connectedViewControllers: [UIViewController] {get} } extension UIPageViewControllerDataSourceWithConnections { func

Default trait method implementation for all trait objects

不羁的心 提交于 2019-12-11 08:28:38
问题 I have a trait MyTrait , and I want all trait objects &MyTrait to be comparable to each other and to nothing else. I have that now based on How to test for equality between trait objects?. The problem is that I need to use MyTraitComparable everywhere, instead of MyTrait . Is there a way to get around this? use std::any::Any; trait MyTrait {} trait MyTraitComparable: MyTrait { fn as_any(&self) -> &Any; fn equals(&self, other: &MyTraitComparable) -> bool; } impl<S: 'static + MyTrait +

Why does the compiler not see the default code in a protocol?

半城伤御伤魂 提交于 2019-12-02 04:58:56
问题 Edit: I have restated and hopefully clarified this question over here. Now I've added the solution. I've defined a function (see foo() in attached example) as a default function for struct s adopting my protocol . It applies the + operator defined in respect of two other variables which themselves adopt other protocols and + is defined in one of those protocols. The variables are typed using associatedtype s. I get the message: Binary operator '+' cannot be applied to operands of type 'Self

Can an interface method have a body?

孤街醉人 提交于 2019-11-28 04:03:28
I know that an interface is like a 100% pure abstract class. So, it can't have method implementation in it. But, I saw a strange code. Can anyone explain it? Code Snippet: interface Whoa { public static void doStuff() { System.out.println("This is not default implementation"); } } EDIT: My IDE is Intellij Idea 13.1. The project SDK is java 7 <1.7.0_25>. The IDE is not showing any compiler error. But, When I compile the code at command line I am getting the following message. Whoa.java:2: error: modifier static not allowed here public static void doStuff() { ^ From Java 8 you can define static

What does an ampersand after this assignment operator mean?

左心房为你撑大大i 提交于 2019-11-27 17:35:47
I was reading through this nice answer regarding the "Rule-of-five" and I've noticed something that I don't recall seeing before: class C { ... C& operator=(const C&) & = default; C& operator=(C&&) & = default; ... }; What is the purpose of the & character placed in front of = default for the copy assignment operator and for the move assignment operator? Does anyone have a reference for this? It's part of a feature allowing C++11 non-static member functions to differentiate between whether they are being called on an lvalues or rvalues. In the above case, the copy assignment operator being

Can an interface method have a body?

徘徊边缘 提交于 2019-11-27 00:15:58
问题 I know that an interface is like a 100% pure abstract class. So, it can't have method implementation in it. But, I saw a strange code. Can anyone explain it? Code Snippet: interface Whoa { public static void doStuff() { System.out.println("This is not default implementation"); } } EDIT: My IDE is Intellij Idea 13.1. The project SDK is java 7 <1.7.0_25>. The IDE is not showing any compiler error. But, When I compile the code at command line I am getting the following message. Whoa.java:2:

What does an ampersand after this assignment operator mean?

*爱你&永不变心* 提交于 2019-11-26 19:06:44
问题 I was reading through this nice answer regarding the "Rule-of-five" and I've noticed something that I don't recall seeing before: class C { ... C& operator=(const C&) & = default; C& operator=(C&&) & = default; ... }; What is the purpose of the & character placed in front of = default for the copy assignment operator and for the move assignment operator? Does anyone have a reference for this? 回答1: It's part of a feature allowing C++11 non-static member functions to differentiate between