Redefining C++ function using a struct -


let's say, have following in "foo.hpp". have function same name struct.

int foo(int i);  struct foo{  foo(int i){ value = i;}  operator int() {return value;}  int value; }; 

if call in main so:

int main() {  std::cout << foo(1) << std::endl; // function called, not struct, why?? } 

why compiler link function , not struct? if possible, how change struct linked, not function?

ultimately, overwrite function in library adding header file , overwriting function struct. way, hoping change 1 specific function, continue using other library functions , not alter library code @ same time.

justification

according c++ standard n4431 § 3.3.10/2 name hiding [basic.scope.hiding] (emphasis mine):

a class name (9.1) or enumeration name (7.2) can hidden name of a variable, data member, function, or enumerator declared in same scope. if class or enumeration name , variable, data member, function, or enumerator declared in same scope (in order) same name, class or enumeration name is hidden wherever the variable, data member, function, or enumerator name visible.

solution

put either struct foo or function foo in own namespace.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -