LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   code substitutions (https://www.linuxquestions.org/questions/programming-9/code-substitutions-891356/)

Loarn 07-12-2011 03:41 PM

code substitutions
 
GNU g++ seems to try to automatically match STL templates to patterns
in your code. E.g. if you code a swap looking function it substitutes
std::swap, is this so or am I inadvertently specifying a template ?

dwhitney67 07-13-2011 08:19 AM

Avoid specifying "using namespace std" in your code!

Also, you can always reference functions using the :: operator. Here's an example that you can play with:
Code:

#include <algorithm>
#include <iostream>

template <typename T>
void swap(T& x, T& y)
{
  std::cout << "I'm on strike!" << std::endl;
}

int main()
{
  int x = 10;
  int y = 20;

  std::cout << "x = " << x << ", y = " << y << std::endl;

  std::swap(x, y);  // calls the C++ library.

  std::cout << "x = " << x << ", y = " << y << std::endl;

  ::swap(x, y);  // calls the swap() function defined above.

  std::cout << "x = " << x << ", y = " << y << std::endl;
}


Loarn 07-14-2011 06:07 PM

whoops
 
I(Loarn) was trying to get the compiler to use my code and not what it considered a match
from the template supply, What finally dawned on me, I have been away for awhile, was that
is was not necessarily using the template but since I had #included <algorithm> and was
compiling with the -E flag, it was including the entire algorithm header file in the
assembler stage of the compile.
Thanks for your assistance, please ignore the misspelling I was schooled in the progressive
era.


All times are GMT -5. The time now is 07:22 AM.