operator-overloading

Operator overloading in Python: handling different types and order of parameters [duplicate]

北城余情 提交于 2020-04-08 00:50:08
问题 This question already has an answer here : Python commutative operator override (1 answer) Closed 2 years ago . I have a simple class that helps with mathematical operations on vectors (i.e. lists of numbers). My Vector can be multiplied by other instances of Vector or a scalar ( float or int ). In other, more strongly typed, languages I would create a method to multiply two vector s and a separate method to multiply a vector by and int / float . I'm still pretty new to Python and am not sure

The void(), the comma operator (operator,) and the impossible (?) overloading

感情迁移 提交于 2020-02-27 04:22:50
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

The void(), the comma operator (operator,) and the impossible (?) overloading

落花浮王杯 提交于 2020-02-27 04:21:32
问题 Consider the following struct: struct S {}; In C++14, the definition below is valid: constexpr auto f() { return S{}, 'c'; } As well as the following one: constexpr auto f() { return S{}, void(); } Now, consider the following, working snippet that involves the first of the two definitions: #include<type_traits> struct S {}; constexpr int operator,(S, char) { return 42; } constexpr auto f() { return S{}, 'c'; } int main() { constexpr int i{f()}; static_assert(i == 42, "!"); static_assert(std:

Is overloading operator() for a reconstruction a good practice?

前提是你 提交于 2020-02-24 14:35:29
问题 I was thinking of the following scenario: class A { private: std::string id; std::array<std::string, 128> data; public: A(const std::string& id) : id(id) {} A(const A& other) : id(other.id), data(other.data) {} virtual ~A(){} //to override the intern data A& operator=(const A& other) { this->data = other.data; return *this; } //to override the whole element A& operator()(const A& other) { this->id = other.id; this->data = other.data; return *this; } }; As you can see, my idea was to use

Which programming languages, other than C++, support operator overloading?

こ雲淡風輕ζ 提交于 2020-02-21 13:00:08
问题 I know C++. Anything other? 回答1: Wikipedia's article on Operator Overloading features a list of languages that support it. 回答2: I'm amused thinking about it. Any C compiler for example, must tell between an int *c; and double d = (int) f * (float) g; . In the former case, * indicates c is a pointer to int while in the latter case it is a multiplication operator. Though not exported outside the compiler itself, C appears to have operator overloading feature. C++ exported it outside the

Which programming languages, other than C++, support operator overloading?

痴心易碎 提交于 2020-02-21 12:59:11
问题 I know C++. Anything other? 回答1: Wikipedia's article on Operator Overloading features a list of languages that support it. 回答2: I'm amused thinking about it. Any C compiler for example, must tell between an int *c; and double d = (int) f * (float) g; . In the former case, * indicates c is a pointer to int while in the latter case it is a multiplication operator. Though not exported outside the compiler itself, C appears to have operator overloading feature. C++ exported it outside the

Which programming languages, other than C++, support operator overloading?

白昼怎懂夜的黑 提交于 2020-02-21 12:57:38
问题 I know C++. Anything other? 回答1: Wikipedia's article on Operator Overloading features a list of languages that support it. 回答2: I'm amused thinking about it. Any C compiler for example, must tell between an int *c; and double d = (int) f * (float) g; . In the former case, * indicates c is a pointer to int while in the latter case it is a multiplication operator. Though not exported outside the compiler itself, C appears to have operator overloading feature. C++ exported it outside the

Which programming languages, other than C++, support operator overloading?

天大地大妈咪最大 提交于 2020-02-21 12:55:39
问题 I know C++. Anything other? 回答1: Wikipedia's article on Operator Overloading features a list of languages that support it. 回答2: I'm amused thinking about it. Any C compiler for example, must tell between an int *c; and double d = (int) f * (float) g; . In the former case, * indicates c is a pointer to int while in the latter case it is a multiplication operator. Though not exported outside the compiler itself, C appears to have operator overloading feature. C++ exported it outside the

Can't use overloaded comparison operator with Catch test

拜拜、爱过 提交于 2020-02-05 04:25:00
问题 I have a simple unit-test using Catch 2.11.1: #define CATCH_CONFIG_MAIN #include "catch.hpp" #include <utility> #include <any> namespace A::B { namespace C { struct S { }; } using type = std::pair<C::S, std::any>; } inline bool operator==(A::B::type const&, A::B::type const&) { return true; } TEST_CASE("test", "[test]") { auto t1 = std::make_pair(A::B::C::S(), std::any()); auto t2 = std::make_pair(A::B::C::S(), std::any()); REQUIRE(t1 == t2); } The above simple programs generates the

Can't use overloaded comparison operator with Catch test

寵の児 提交于 2020-02-05 04:24:53
问题 I have a simple unit-test using Catch 2.11.1: #define CATCH_CONFIG_MAIN #include "catch.hpp" #include <utility> #include <any> namespace A::B { namespace C { struct S { }; } using type = std::pair<C::S, std::any>; } inline bool operator==(A::B::type const&, A::B::type const&) { return true; } TEST_CASE("test", "[test]") { auto t1 = std::make_pair(A::B::C::S(), std::any()); auto t2 = std::make_pair(A::B::C::S(), std::any()); REQUIRE(t1 == t2); } The above simple programs generates the