why didn't the positive terms get displayed in this asbolute program

前端 未结 4 788
鱼传尺愫
鱼传尺愫 2021-01-14 18:20

Let\'s start with this demo

#include 
using namespace std;

template 
void abs(T number)
{
   if (number < 0)
        numbe         


        
4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-14 19:03

    Introduce your own namespace

    #include 
    #include  // just for testing ADL of 'abs'
    
    namespace ns // introduce namespace
    {
      template
      void abs(T number)
      {
        if (number < 0)
        {
          number = -number;
        }
    
        std::cout << "The absolute value of the number is " << number << std::endl;
      }
    }
    
    int main()
    {
      int num1 = 1;
      int num2 = 2;
      double num3 = -2.1333;
      float num4 = -4.23f;
    
      using ns::abs; // use abs from the namespace just introduced above
    
      abs(num1);
      abs(num2);
      abs(num3);
      abs(num4);
    
      return 0;
    }
    

提交回复
热议问题