operator-overloading

Error: use of overloaded operator '[]' is ambiguous while building for i386

送分小仙女□ 提交于 2021-02-09 08:34:02
问题 Consider the following code: #include <stdio.h> #include <stdint.h> class test_class { public: test_class() {} ~test_class() {} const int32_t operator[](uint32_t index) const { return (int32_t)index; } operator const char *() const { return "Hello World"; } }; int main(void) { test_class tmp; printf("%d\n", tmp[3]); return 0; } When I use command clang++ -arch i386 test.cc to build those codes, it yields the following on clang++ (Apple LLVM version 9.1.0 (clang-902.0.39.1)): test.cc:24:21:

Error: use of overloaded operator '[]' is ambiguous while building for i386

a 夏天 提交于 2021-02-09 08:29:32
问题 Consider the following code: #include <stdio.h> #include <stdint.h> class test_class { public: test_class() {} ~test_class() {} const int32_t operator[](uint32_t index) const { return (int32_t)index; } operator const char *() const { return "Hello World"; } }; int main(void) { test_class tmp; printf("%d\n", tmp[3]); return 0; } When I use command clang++ -arch i386 test.cc to build those codes, it yields the following on clang++ (Apple LLVM version 9.1.0 (clang-902.0.39.1)): test.cc:24:21:

Overloading operator for generics C# [duplicate]

谁都会走 提交于 2021-02-08 16:55:20
问题 This question already has answers here : C# Generic Operators (5 answers) Closed 5 years ago . I would like to create a procedure class that supports connecting to another procedure like so: a|b|c|d (this should result a procedure takes a's input type, and give d's output type) class Procedure<I,O> { public Func<I,O> func; public Procedure(Func<I, O> func) { this.func = func; } public static Procedure<I, O> operator| (Procedure<I, M> proc1, Procedure<M, O> proc2) { return new Procedure<I,O>(

overloaded array subscript [] operator slow

寵の児 提交于 2021-02-08 14:02:23
问题 I have written my own Array class in c++ and overloaded the array subscript [] operator, code: inline dtype &operator[](const size_t i) { return _data[i]; } inline dtype operator[](const size_t i) const { return _data[i];} where _data is a pointer to the memory block containing the array. Profiling shows that this overloaded operator alone is taking about 10% of the overall computation time (on a long monte carlo simulation, and I am compiling using g++ with maximum optimization). This seems

Does “operator true” in c# have exactly two places it can be used?

孤街醉人 提交于 2021-02-08 13:29:17
问题 c# lets you override "operator true" and "operator false" for a class: class Foo { bool Thing; Foo(bool thing) { Thing = thing; } public static bool operator true(Foo foo) => foo.Thing; public static bool operator false(Foo foo) => !foo.Thing; } and it sort of works. You can say Foo foo = new Foo(true); if (foo) Stuff(); string s = foo ? "yes" : "no"; But you can't say Foo foo = new Foo(true); bool boo = true; if (boo && foo) Stuff(); if (boo & foo) Stuff(); if (boo & (foo == true)) Stuff();

How to define the 'AT-POS' method?

為{幸葍}努か 提交于 2021-02-08 13:10:33
问题 I defined the AT-POS method for a class and exported the [] operator. When I used [] on the instance of that class, however, the compiler ignored the operator defined by me. Here is the code: unit module somelib; class SomeClass is export { method AT-POS(@indices) { say "indices are {@indices.perl}" } } multi postcircumfix:<[ ]> (SomeClass:D $inst, *@indices) is export { $inst.AT-POS(@indices) } #! /usr/bin/env perl6 use v6.c use lib "."; use somelib; my $inst = SomeClass.new; $inst[3, 'hi'];

istream operator overloading c++

时光毁灭记忆、已成空白 提交于 2021-02-08 08:59:42
问题 i'm trying to do a simple istream operator overloading, but for some reason, once entering this function, the program enters an infinite loop. please help! my code: #include <iostream> #include <string> using namespace std; class date{ int m_day,m_month,m_year; public: date(int day=1,int month=1,int year=2000){ //constructor if (day>0 && day<32 && month>0 && month<13){ m_day =day; m_month=month; m_year=year; } } friend ostream& operator<< (ostream& out, const date& d); friend istream&

checking for self-assignment in fortran overloaded assignment

倾然丶 夕夏残阳落幕 提交于 2021-02-07 21:11:20
问题 I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this type polynomial private type(monomial),dimension(:),allocatable :: term double precision,dimension(:),allocatable :: coef integer :: nterms=0 contains ... end type polynomial interface assignment(=) module procedure :: polynomial_assignment end interface ... contains elemental

checking for self-assignment in fortran overloaded assignment

喜夏-厌秋 提交于 2021-02-07 21:04:10
问题 I am trying to implement a polynomial class with fortran 2003, with overloaded arithmetic operations and assignments. The derived type maintains allocatable list of term definitions and coefficients, like this type polynomial private type(monomial),dimension(:),allocatable :: term double precision,dimension(:),allocatable :: coef integer :: nterms=0 contains ... end type polynomial interface assignment(=) module procedure :: polynomial_assignment end interface ... contains elemental

Is it possible to overload operators for native datatypes?

左心房为你撑大大i 提交于 2021-02-07 17:27:46
问题 For example, if I try to do: a_string + an_int ... where a_string is type 'str' and an_int is type 'int', or: an_int + a_string There would be a TypeError because there is no implicit conversion of the types. I understand that if I were using my own subclasses of int and string, I would be able to overload the __add__() method in my classes to achieve this. However, out of curiosity, I would like to know: would it be possible to overload the + operator in the class definitions of int and str