天道酬勤,学无止境

operator-overloading

How to overload Lua string subscript operator?

This: debug.getmetatable("").__index = function (s, i) return s:sub(i, i) end and this: debug.getmetatable("").__index = _proc_lua_read does not work.

2022-01-19 01:19:45    分类:问答    string   lua   operator-overloading

Implicit conversion of collections

If I define an explicit conversion operator between two types, shouldn't it follow that I can explicitly convert between collections of those types? Ie. public static explicit operator FooEntity(Entity entity) { FooEntity e = new FooEntity(entity); return e; } And thus I could do this, IEnumerable<Entity> entities = GetEntities(); IEnumerable<FooEntity> fooEntities = (IEnumerable<FooEntity>)entities; or IEnumerable<FooEntity> fooEntities = entities as IEnumerable<FooEntity> Is this possible somehow or do I also have to create my own operator to convert between the collections? I am getting a

2022-01-19 00:33:02    分类:问答    c#   operator-overloading   ienumerable

Using overloaded operator[] via an accessor function

I have an accessor function that returns a const reference to a type (std::map) ... ... myMap_t const& getMap() const {return paramMap;} The type has an overloaded [] operator. What is the syntax to then use the [] operator directly from the getter function in a way like the following, but that actually works. parameter = contextObj.getMap()[key]; Error message is: context.cpp:35: error: passing 'const std::map< std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std

2022-01-18 07:05:45    分类:问答    c++   operator-overloading   accessor

Lua override # for strings

I'm trying to imlement my own length method for strings in Lua. I have successfully overriden len() method for string, but I have no idea how to do this for # operator. orig_len = string.len function my_len(s) print(s) return orig_len(s) end string.len = my_len abc = 'abc' If I call: print(abc:len()) It outputs: abc 3 But print(#abc) Outputs only '3' and that means it called original length function instead of mine. Is there a way to make # call my length function?

2022-01-18 06:28:10    分类:问答    lua   operator-overloading   metatable   meta-method

Global overloading of == and != for floating-points

Is it a bad practice to overload global operator == and != for floating points ? I'm using fast floating-points in a game environement, and i was thinking about using fuzzy comparison everywhere as i can't imagine a situation where i don't expect extremely close numbers not to be equals. Any advice ?

2022-01-18 06:26:06    分类:问答    c++   operator-overloading   numerical

C++ overloading operator<< in a template class [duplicate]

This question already has answers here: Closed 9 years ago. Possible Duplicate: overloading friend operator<< for template class I'm trying to overload operator<< for a template class but I'm getting errors... Final(fixed) code: template<class T> class mytype { T atr; public: mytype(); mytype(T); mytype(mytype&); T getAtr() const; T& operator=(const T&); template<class U> friend ostream& operator<<(ostream&,const mytype<U>&); }; template<class T> mytype<T>::mytype() { atr=0; } template<class T> mytype<T>::mytype(T value) { atr=value; } template<class T> mytype<T>::mytype(mytype& obj) { atr=obj

2022-01-18 03:50:31    分类:问答    c++   class   templates   operator-overloading

Can you impose object precedence for overloaded operators in Python?

Say I have two Python classes which both define the add and radd operator overloads, and I add one instance of one class to another instance of another class. The chosen implementation depends on the order in which the items are added (Python looks for an add method on the LHS first, etc). Is it possible for me to define a precedence on which object's implementation is preferred? I want, for example, that radd is called on the RHS if its precedence is higher than that of the LHS. I really want to do this for all overloaded operators, so a more general solution is what I'm eventually after.

2022-01-17 18:36:33    分类:问答    python   operator-overloading   operator-precedence

C++ friend function template overloading and SFINAE different behaviors in clang++, g++, vc++ (C++14 mode)

So, the following code builds and runs successfully under clang++ (3.8.0), but fails both under g++ (6.3.0) and vc++ (19.10.24903.0). Both g++ and vc++ complain about redefinition of operator&&. Does anyone know which compiler is at fault here. For the compilers that fails to compile the code, what would be the workarounds for the compilation error? #include <functional> #include <iostream> template <typename T> struct awaitable { friend awaitable<void> operator&&(awaitable a1, awaitable a2) { std::cout << "operator&&(awaitable a1, awaitable a2) - T: " << typeid(T).name() << std::endl; return

2022-01-17 17:01:06    分类:问答    c++   operator-overloading   sfinae   friend-function   function-templates

is the following new overload leaking memory?

I have encountered the following code: class a { public: void * operator new(size_t l, int nb); double values; }; void *a::operator new (size_t l,int n) { return new char[l+ (n>1 ? n - 1 : 0)*sizeof(double)]; } From what I get it is then used to have an array like structure that start at "values": double* Val = &(p->a->values) + fColumnNumber; My question is : is there a memory leak? I am very new to overloading new operator, but I'm pretty sure that the memory allocated is not deallocated properly. Also does that mean I can never create a "a" class on the stack? thanks

2022-01-17 12:40:44    分类:问答    c++   memory-management   operator-overloading   new-operator

Overload << operator to change " " to "\n"

I am trying to overload << operator. For instance cout << a << " " << b << " "; // I am not allowed to change this line is given I have to print it in format <literal_valueof_a><"\n> <literal_valueof_b><"\n"> <"\n"> I tried to overload << operator giving string as argument but it is not working. So I guess literal " " is not a string. If it is not then what is it. And how to overload it? Kindly help; Full code //Begin Program // Begin -> Non - Editable #include <iostream> #include <string> using namespace std; // End -> Non -Editable //----------------------------------------------------------

2022-01-17 02:18:17    分类:问答    c++   string   operator-overloading   cout   ostream