How to overload operator + for const char* and int


I know this is silly and ugly, but I'm migrating some code automatically. My source language allows implicit conversion between strings and ints, and for example this is allowed:

var = "hello " + 2 print(var) # prints "hello 2"

How can I in C++ overload the + operator for const char* and int? I'm getting the error:


error: ‘std::string operator+(char* const&, int)’ must have an argument of class or enumerated type



<h3>What you are asking for is illegal</h3>

To legally overload an operator at least one of the operands involved has to be a <em>user-defined type</em>. Since neither char* nor int is <em>user-defined</em>, what you are trying to accomplish isn't possible.

This, what you are trying to do, is intentionally, and explicitly, disallowed in the standard. Don't you think it would be weird if suddenly 1+3 = 42 because someone "clever" have defined an overload for operator+(int, int)?

<hr /><h3>What does the Standard say? (<a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf" rel="nofollow">n3337</a>)</h3> <blockquote> <strong>Operators in expressions</strong> [over.match.oper]


If no operand of an operator in an expression has a type that is a class or an enumeration, the operator is assumed to be a built-in operator and interpreted according to Clause 5.

If either operand has a type that is a class or an enumeration, a user-defined operator function might be declared that implements this operator or a user-defined conversion can be neccessary to convert the operand to a type that is appropriate for a built-in operator.

</blockquote> </blockquote>

( <strong>Note</strong>: The wording is the same in both <em>C++03</em>, and the next revision of the standard; <em>C++14</em> )


  • Should I use std::shared pointer to pass a pointer?
  • Violation of section 4.3 of the Developer Distribution Agreement
  • Redistribution licensing of .NET classes [closed]
  • Why java statement evaluation is happening like these ?
  • Check for overflow condition in an arithmetic operation [duplicate]
  • Is Android capable of managing microphone/input volume?
  • Einsum optimize fails for basic operation
  • Generate a unique string based on a pair of strings
  • runtime-check whether an instance (Base*) override a parent function (Base::f())
  • Building Qt project for C++11 standard
  • C++ friend class std::vector
  • How do I mock an exported typescript function in a jasmine test?
  • Error in installing package: fatal error: stdlib.h: no such file or directory
  • Invalid Date on validation Date of js
  • Appending Character to Character Array In C
  • C: Incompatible pointer type initializing
  • d3 v4 drag and drop with TypeScript
  • Why does access(2) check for real and not effective UID?
  • Illegal mix of collations for operation for date/time comparison
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • output of program is not same as passed argument
  • MySQL WHERE-condition in procedure ignored
  • Convert array of 8 bytes to signed long in C++
  • Rearranging Cells in UITableView Bug & Saving Changes
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • Circular dependency while pushing http interceptor
  • Is there a mandatory requirement to switch app.yaml?
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Hits per day in Google Big Query
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • apache spark aggregate function using min value
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal