operator-overloading

Is it legal to use variadic templates in operator overloading?

强颜欢笑 提交于 2020-01-30 04:17:30
问题 I would like to be able to write something along these lines: struct bar {}; template <typename ... Args> bar operator+(bar, Args ...) {} I just checked with clang/gcc and the overloaded operator is picked up both by binary expressions ( a+b ) and unary expressions ( +a ), as I would expect. However operators are more restricted than normal functions, in the sense that - for instance - you cannot overload operator+() with three arguments. Is the usage above legal and portable? EDIT To give a

Why does + work with Strings in Java?

一世执手 提交于 2020-01-28 09:25:11
问题 Java can't do operator overloading, but + works okay for String and Integer and some other classes. How is this possible? update: Why does this work? Integer i = 4; Integer p = 5; System.out.println(i*p); // prints 20 回答1: + is not an example of operator overloading. + is built into the language as a concatentation operator and an arithmetic-addition operator. What this means is that a person writing a program with Java cannot overload operators, but as far as the grammar of the Java language

C++ Array Subscript Operator Template

让人想犯罪 __ 提交于 2020-01-28 06:34:32
问题 After trying to make access to a storage class a little easier, I ended up in a situation that I don't have a lot of knowledge on. And, finding people that are trying to do the same thing as me isn't easy. What I'm trying to do, is have a class that stores an array of values as strings internally, but allows simple type casting from the user's end. What I had planned on doing is use the array subscript operator to return whichever type they specify through a template. Although, it sounds a

Using overloaded operator[] via an accessor function

陌路散爱 提交于 2020-01-24 08:48:27
问题 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,

Using overloaded operator[] via an accessor function

梦想的初衷 提交于 2020-01-24 08:46:19
问题 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,

C++ template for function call operator

*爱你&永不变心* 提交于 2020-01-24 03:11:06
问题 I tried to use template for function call operator overload as in the following program: #include <stdio.h> struct Apple { template <typename tn> tn value (); template <typename tn> tn operator () (); }; template <> int Apple::value () { return 10; } template <> int Apple::operator () () { return 10; } int main() { Apple apple; printf("Value : %d\n", apple<int>()); printf("Value : %d\n", apple.value<int>()); return 0; } While value function call in the second print does not show any error the

lvalue required as left operand of assignment - Array

房东的猫 提交于 2020-01-24 00:29:32
问题 Below is the snippet of code where the error lies, the line a[i][j] = m[i][j] + w[i][j]; returns an error lvalue required as left operand of assignment I can't find an answer that applies to arrays, my Matrix is defined as follows: Matrix::Matrix() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) coords[i][j] = 0.0f; } const Matrix operator+(const Matrix &m, const Matrix &w) { Matrix a; for (int i = 0; i < 3; i++) for (int j = 0; j < 4 ; j++) a[i][j] = m[i][j] + w[i][j]; // <-----

lvalue required as left operand of assignment - Array

為{幸葍}努か 提交于 2020-01-24 00:29:07
问题 Below is the snippet of code where the error lies, the line a[i][j] = m[i][j] + w[i][j]; returns an error lvalue required as left operand of assignment I can't find an answer that applies to arrays, my Matrix is defined as follows: Matrix::Matrix() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) coords[i][j] = 0.0f; } const Matrix operator+(const Matrix &m, const Matrix &w) { Matrix a; for (int i = 0; i < 3; i++) for (int j = 0; j < 4 ; j++) a[i][j] = m[i][j] + w[i][j]; // <-----

ambiguous operator[] in variadic template

十年热恋 提交于 2020-01-23 07:41:46
问题 I'm trying to compile this example, where a variadic class template inherits from a variadic amount of bases, each of which implements a different operator[] : #include <iostream> template <typename T> struct Field { typename T::value_type storage; typename T::value_type &operator[](const T &c) { return storage; } }; template<typename... Fields> struct ctmap : public Field<Fields>... { }; int main() { struct age { typedef int value_type; }; struct last_name { typedef std::string value_type; }

operator overloading [][] 2d array c++

杀马特。学长 韩版系。学妹 提交于 2020-01-22 15:04:27
问题 I have a 2D array and I want to define a function that returns the value of the index that the user gives me using operator overloading. In other words: void MyMatrix::ReturnValue() { int row = 0, col = 0; cout << "Return Value From the last Matrix" << endl; cout << "----------------------------------" << endl; cout << "Please Enter the index: [" << row << "][" << col << "] =" << ((*this).matrix)[row][col] << endl; } The operation ((*this).matrix)[row][col] should return an int . I have no