Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. #define style templates and float

#define style templates and float

Scheduled Pinned Locked Moved C / C++ / MFC
c++wpfhelptutorialquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    J B 0
    wrote on last edited by
    #1

    Hi guys, I tried to compile and run a #define type templates example code in Visual C++:

    #include 
    
    #define define_max(type)	\
    	type max(type d1, type d2) { \
    		if (d1 > d2)	\
    			return (d1); \
    		return (d2);	\
    	}
    
    define_max(int);
    define_max(float);
    define_max(char);
    
    int main(void)
    {
    	int i = max(100, 800);
    	char ch = max('A', 'Q');
    	float f = max(3.5, 8.7);
    
    	return 0;
    }
    

    and I am getting compile error ambiguous call to overloaded function on the line

    float f = max(3.5, 8.7);
    

    If I replace float with double, it will compile and run ok. Does anyone know why that is? Is there some kind of limitation on uses of float in Visual C++? Thanks alot

    D V 2 Replies Last reply
    0
    • J J B 0

      Hi guys, I tried to compile and run a #define type templates example code in Visual C++:

      #include 
      
      #define define_max(type)	\
      	type max(type d1, type d2) { \
      		if (d1 > d2)	\
      			return (d1); \
      		return (d2);	\
      	}
      
      define_max(int);
      define_max(float);
      define_max(char);
      
      int main(void)
      {
      	int i = max(100, 800);
      	char ch = max('A', 'Q');
      	float f = max(3.5, 8.7);
      
      	return 0;
      }
      

      and I am getting compile error ambiguous call to overloaded function on the line

      float f = max(3.5, 8.7);
      

      If I replace float with double, it will compile and run ok. Does anyone know why that is? Is there some kind of limitation on uses of float in Visual C++? Thanks alot

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      This is just a guess, but try replacing the four occurrences of max() with something like max1().


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      1 Reply Last reply
      0
      • J J B 0

        Hi guys, I tried to compile and run a #define type templates example code in Visual C++:

        #include 
        
        #define define_max(type)	\
        	type max(type d1, type d2) { \
        		if (d1 > d2)	\
        			return (d1); \
        		return (d2);	\
        	}
        
        define_max(int);
        define_max(float);
        define_max(char);
        
        int main(void)
        {
        	int i = max(100, 800);
        	char ch = max('A', 'Q');
        	float f = max(3.5, 8.7);
        
        	return 0;
        }
        

        and I am getting compile error ambiguous call to overloaded function on the line

        float f = max(3.5, 8.7);
        

        If I replace float with double, it will compile and run ok. Does anyone know why that is? Is there some kind of limitation on uses of float in Visual C++? Thanks alot

        V Offline
        V Offline
        vcplusplus
        wrote on last edited by
        #3

        3.5 and 8.7 are consider constant doubles. When I compile the code below, VC gives me a warning for "a" but not for "b"float a = 2.4; warning C4305: 'initializing' : truncation from 'const double' to 'float' float b = 2.4f;

        J 1 Reply Last reply
        0
        • V vcplusplus

          3.5 and 8.7 are consider constant doubles. When I compile the code below, VC gives me a warning for "a" but not for "b"float a = 2.4; warning C4305: 'initializing' : truncation from 'const double' to 'float' float b = 2.4f;

          J Offline
          J Offline
          J B 0
          wrote on last edited by
          #4

          vcplusplus wrote: float a = 2.4;warning C4305: 'initializing' : truncation from 'const double' to 'float'float b = 2.4f; It makes a difference. I suppose the appended f tells the compiler to treat the numbers as float type? Thanks.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups