operator-overloading

Implicit conversion to std::string [duplicate]

只愿长相守 提交于 2019-12-30 08:05:10
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Overload resolution failure when streaming object via implicit conversion to string I know it's not such a good idea to do this, but I really want to know the reason why the code below does not compile (i.e. why there is "no acceptable conversion"): #include <iostream> #include <string> class Test { public: operator std::string () const; }; Test::operator std::string () const { return std::string("Test!"); } int

Implicit conversion to std::string [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-30 08:05:02
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Overload resolution failure when streaming object via implicit conversion to string I know it's not such a good idea to do this, but I really want to know the reason why the code below does not compile (i.e. why there is "no acceptable conversion"): #include <iostream> #include <string> class Test { public: operator std::string () const; }; Test::operator std::string () const { return std::string("Test!"); } int

Implicit conversion to std::string [duplicate]

梦想与她 提交于 2019-12-30 08:05:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Overload resolution failure when streaming object via implicit conversion to string I know it's not such a good idea to do this, but I really want to know the reason why the code below does not compile (i.e. why there is "no acceptable conversion"): #include <iostream> #include <string> class Test { public: operator std::string () const; }; Test::operator std::string () const { return std::string("Test!"); } int

Operator Overloading in Clojure

守給你的承諾、 提交于 2019-12-30 08:04:26
问题 Even looking closely over documentation on Clojure, I do not see any direct confirmation as to whether or not Clojure supports operator overloading. If it does, could someone provide me with a quick snipplet of how to overload, let's say, the "+" operator to delegate to some predefined method that we can call myPlus . I am very new to Clojure, so someone's help here would be greatly appreciated. 回答1: Clojure's (as any Lisp's) operators are plain functions; you can define an "operator" like a

Overloading + operator in F#

寵の児 提交于 2019-12-30 06:47:16
问题 So i have this: open System open System.Linq open Microsoft.FSharp.Collections type Microsoft.FSharp.Collections.List<'a> with static member (+) (First : List<'a>) (Second : List<'a>) = First.Concat(Second) let a = [1; 2; 3; 4; 54; 9] let b = [3; 5; 6; 4; 54] for x in List.(+) a b do Console.WriteLine(x) and I want to convert the last line into for x in a + b do Console.WriteLine(x) but doing so gives me a The type 'int list' does not support any operands named '+' The documentation and

Is it possible to overload from/import in Python?

拥有回忆 提交于 2019-12-30 06:18:27
问题 Is it possible to overload the from/import statement in Python? For example, assuming jvm_object is an instance of class JVM , is it possible to write this code: class JVM(object): def import_func(self, cls): return something... jvm = JVM() # would invoke JVM.import_func from jvm import Foo 回答1: This post demonstrates how to use functionality introduced in PEP-302 to import modules over the web. I post it as an example of how to customize the import statement rather than as suggested usage ;)

Should operators be declared as non-member non-template friends

跟風遠走 提交于 2019-12-30 05:45:45
问题 Consider this question, which is about the following code not compiling: std::vector<int> a, b; std::cout << (std::ref(a) < std::ref(b)); It doesn't compile because the vector comparison operators for vector are non-member function templates, and implicit conversions aren't allowed to be considered. However, if the operators were instead written as non-member non-template, friend functions: template <class T, class Allocator = std::allocator<T>> class vector { // ... friend bool operator<

Implicit method group conversion gotcha (Part 2)

别等时光非礼了梦想. 提交于 2019-12-30 03:57:07
问题 Simplified from this question and got rid of possible affect from LinqPad(no offsensive), a simple console application like this: public class Program { static void M() { } static void Main(string[] args) { Action a = new Action(M); Delegate b = new Action(M); Console.WriteLine(a == b); //got False here Console.Read(); } } The "false" results from the operator ceq in CIL of the code above(visit the original question for details). So my questions are: (1) Why == is translating to ceq instead

Overloading global operator new/delete in C++

为君一笑 提交于 2019-12-30 03:17:09
问题 I am trying to overload the global operator new and delete for a performance sensitive application. I have read the concerns described at http://www.informit.com/articles/article.aspx?p=30642&seqNum=3 and the recommendations to use Intel TBB's allocator http://www.intel.com/technology/itj/2007/v11i4/5-foundations/5-memory.htm Since I am overloading new and delete for the first time, I have a few questions. Should I include my new header Allocator.h (or Pre.h) containing the overloaded new

Overloading operator=() with callback

左心房为你撑大大i 提交于 2019-12-29 09:09:29
问题 I have a template Property which wraps a data and provides other services. This template is specialized for basic data (float, int_32, bool...) and there exists another specilization for vectors of basic types. Something like: template<typename T> Property : public PropertyBase { public: // Base types specilization. void operator=(T const & data); } template<typename T> Property<std::vector<T> > : public PropertyBase { public: // Specilization for vectors of base types. T const & operator[]