14

I was wondering if there was a simple way to write an alias of a c++ class function. For instance, if I have some list container object, a logical function would be

    int list::length() { return len; }

But another logical alias that programmers might use could be

    int list::size() { return len; }

So, instead of writing both functions with their full body, is there any way to make list::size() an alias of list::length() such that it isn't a duplicate when compiled, but rather references the same function?

I've read that you can do this with #define, but I don't want to cause any confusion with other code-names somewhere totally out of scope (i.e. a 'size' variable).
I've also read that function pointers can fix it, but that isn't exactly an alias (since it has to apply de-referencing), nor can function pointers be given a declaration, giving it a confusing help-line to users (I would think), plus the confusion if ever I need to nest my code inside another object (I have to adjust the scope).

One of my guesses is, will the following be taken as a direct function alias by most optimizing compilers:

    inline int list::length() { return len; }
    inline int list::size() { return length(); }

Or, is there any strict 'alias' syntax for c++? (I couldn't find any - wasn't sure)
So then, what would be the most efficient way of doing this?

EDIT: I've accepted the answer simply to wrap up the question, since it's only a curiosity of mine. Anyone with good information, please add comments or answer, and I may even change my answer.

Codesmith
  • 5,779
  • 5
  • 38
  • 50
  • Aliasing in C++ is usually done by references, which work like pointers and add the need for dereferencing. – imreal Nov 10 '12 at 01:57
  • 1
    Your ending code should produce the same assembly for both functions by any modern compiler if I'm correct. – chris Nov 10 '12 at 01:58
  • Good question. I think a step towards it was taken in `C++11` by allowing default and deleted functions. If `int foo() = delete` is ok, why can't `int foo() = int foo_default()` be ok exactly for the reasons you presented? Maybe in the next standard :) As far as I know there is currently no better way than explicitly referencing the other func in the body. – SomeWittyUsername Nov 10 '12 at 07:16
  • @chris, I can understand the compiler will compile the code the same way, but I was hoping to avoid two copies of the same function in the final exe. I'm wondering if there's a way to have the compiler compile exactly one copy of a function that has two names in the source code. ;) – Codesmith Nov 11 '12 at 20:31
  • See this stack overflow on inline. http://stackoverflow.com/questions/145838/benefits-of-inline-functions-in-c – Richard Chambers Nov 11 '12 at 23:59

1 Answers1

6

I would not use the preprocessor and #define to do this. In general preprocessor should be a last resort in C++. See this C++ FAQ on inline functions which also contains a section on the various evils of using macros of the preprocessor.

The approach I would use would be to have a function that will have several different aliases with a more complicated function and interface you would do something like the following:

int list::length(string xString, int iValue) {
  int iReturnValue = 0;  // init the return value
  //  do stuff with xString and iValue and other things
  return iReturnValue;
}

Then do something like the following for an alias.

inline int list::size(string xString, int iValue) {return length(xString, iValue);}

The inline should basically just replace the alias with the actual function call.

See also this stack overflow posting Giving a function implementation more than one name. It provides some reasons why you might not want to do this.

Community
  • 1
  • 1
Richard Chambers
  • 16,643
  • 4
  • 81
  • 106
  • So, I guess my question about that would be, inline functions still stack arguments; would this apply a double-stack? i.e. stack parameters for the `list::size` function and then stack _another_ set of params for the interior `list::length` function call, since the compiler might expect to re-use the `list::size` arguments in ways other than just sending them down again? or do compilers catch that and apply only one stack? I know it's really not much of an optimization on modern systems, but I was just curious.. – Codesmith Nov 11 '12 at 20:25
  • @AUTO, Here is an FAQ on inline functions (http://www.parashift.com/c++-faq/inline-functions.html). The idea of an inline function is that the compiler does not generate a call to the function but rather it takes the source text of the function and compiles it inline replacing the function call with the generated object code of the function. This is why it can lead to code bloat. – Richard Chambers Nov 11 '12 at 23:38