overloading

Why doesn’t Swift call my overloaded method with a more specific type?

左心房为你撑大大i 提交于 2020-01-10 19:43:50
问题 I use Decodable to decode a simple struct from JSON. This works by conforming to a Decodable protocol: extension BackendServerID: Decodable { static func decode(_ json: Any) throws -> BackendServerID { return try BackendServerID( id: json => "id", name: json => "name" ) } } I’d like to be able to call decode with a String , though, so I have added an extension: extension Decodable { static func decode(_ string: String) throws -> Self { let jsonData = string.data(using: .utf8)! let jsonObject

Avoid MATLAB startup warning when overloading buildin functions?

喜欢而已 提交于 2020-01-10 19:42:39
问题 As described here, I created my own figure.m which nicely overloads the built-in figure command. Now, whenever I start MATLAB I get the warning Warning: Function C:\somepath\figure.m has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict. Is there any way to deactivate this warning, given that it is desired behavior in my case? You might say that I should call my function differently instead of overloading, but I do feel for my development

constexpr overloading

[亡魂溺海] 提交于 2020-01-08 16:02:54
问题 Related: Function returning constexpr does not compile I feel like constexpr is limited in usefulness in C++11 because of the inability to define two functions that would otherwise have the same signature, but have one be constexpr and the other not constexpr. In other words, it would be very helpful if I could have, for example, a constexpr std::string constructor that takes constexpr arguments only, and a non-constexpr std::string constructor for non-constexpr arguments. Another example

How to symmetrically implement serialize and deserialize template functions in C++

冷暖自知 提交于 2020-01-07 04:20:22
问题 I want to write a serial of template functions to serialize and deserialize objects. I've finished the serialization part and everything works: #ifndef SERIALIZE_H #define SERIALIZE_H #include <string> #include <vector> #include <unordered_set> #include <unordered_map> #include <memory> inline std::string to_json(int value) { return std::to_string(value); } inline std::string to_json(long value) { return std::to_string(value); } inline std::string to_json(double value) { return std::to_string

Implementing Scalar and Vector Addition for Custom Type with std::transform

谁都会走 提交于 2020-01-06 12:53:09
问题 This looks to me like a basic problem so apologies in advance for duplicate posts. Not sure what the terminology is though. I have a class, say my_data , for storing numbers. It has the basic constructors etc. and it would be handy to support elementary operations such as adding to the elements, e.g. increasing the values in the data. This is the most elementary way to describe the class: #include <stdlib.h> #include <vector> #include <iostream> #include <algorithm> #include <numeric>

Overloading postfix operator doesn't work

那年仲夏 提交于 2020-01-06 09:53:53
问题 #include <iostream> using namespace std; class NumDays { private: int hour; int day; void simplify(); public: NumDays() { day = 0; hour = 0; } void setData(int d, int h) { hour = h; day = d; simplify(); } int getHour() { return hour; } int getDay() { return day; } NumDays operator++(int); NumDays operator--(int); }; void NumDays::simplify() { hour = 8*day + hour; day = hour / 8; hour = hour % 8; } NumDays NumDays::operator++(int) { NumDays obj1; hour++; simplify(); return obj1; } NumDays

Function overloading in C using GCC - functions with mutiple arguments

耗尽温柔 提交于 2020-01-05 11:14:10
问题 In a previous question I found a way to overload functions in C99 when each function only took a single argument. See the answers in: Function overloading in C using GCC - compiler warnings for details. Now that I've found a way to do it with single argument functions I'm wondering how this can be done for functions that take multiple arguments. I assume it will have something to do with __VA_ARGS__ and using ... but I can't seem to find anything that works or even wants to compile. This will

Access properties in View phtml file that have been set in the Controller

落爺英雄遲暮 提交于 2020-01-05 07:30:10
问题 I am working on a small PHP website which is based on MVC. I have a front controller ( front.php ) which loads the controller ( services.php ), runs the action method ( hostingAction() ) and includes the html ( view.phtml ). There is a method call in view.phtml ( $this->renderContent() ) that includes the inner content ( hosting.phtml ). QUESTION: How can I set properties (e.g. $title = 'My Title'; ) in the hostingAction() method and then in view.phtml do <title><?php echo $title ?></title> ?

PHP can I call unexistent function with call_user_func

空扰寡人 提交于 2020-01-05 03:37:11
问题 Here it what I want to do: I have an object class TestObject { public function __call($name,$args){ if($name == 'somemethod'){ print "Yesssss!!!!!!"; } } } than in code i'm calling this method like this: $obj = new TestObject() $obj->somemethod() // should work according to http://www.php.net/manual/en/language.oop5.overloading.php#object.call call_user_func(array('myobject','somemethod')) // this does not work!!! Is there any way to get the last example to work? 回答1: You can use call_user

How do input field methods (text_area, text_field, etc.) get attribute values from a record within a form_for block?

霸气de小男生 提交于 2020-01-05 03:33:06
问题 I have a standard Rails 2.3.5 app with a model called Post. Post has an attribute called url, and the following getter is defined: def url p = 'http://' u = self[:url] u.starts_with?(p) ? u : "#{p}#{u}" end If I load up script/console , I can do Post.first.url and get the desired result (e.g. it returns http://foo.com if the attribute's true value is foo.com) However, if I have a form_for block, and do something like form.text_field :url , it will not return the url with http:// prefixed;