CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2008
    Posts
    55

    function template

    im working on a function template. i just want to know if im doing it right so far. i put comments on parts im not sure of

    #include<iostream>
    using namespace std;

    template <typename isEqualTo = int> // is this a template header with one formal type of parameter
    // how would i write a header for function template isEqualTo
    {
    return arg1==arg2;
    }

    int main(){
    int a;
    int b;
    cout<<"enter two interger values: ";
    cin>>a>>b;
    cout<<a<<"and"<<b<<"are"
    <<( ?"equal":"not equal")<<'\n';
    return 0;
    }

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: function template

    I think you have this in mind

    Code:
    template <typename T = int>
    bool AreEqual(const T arg1, const T arg2)
    {
    return arg1==arg2;
    }
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  3. #3
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: function template

    Quote Originally Posted by souldog
    I think you have this in mind

    Code:
    template <typename T = int>
    bool AreEqual(const T arg1, const T arg2)
    {
    return arg1==arg2;
    }
    You can't have default template arguments with "function" templates. Making that a functor might solve the problem though.

  4. #4
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: function template

    Quote Originally Posted by exterminator
    You can't have default template arguments with "function" templates. Making that a functor might solve the problem though.
    I can't imagine the purpose of a default template argument in this case anyway, since the type appears in the parameter list of the function and can be automatically deduced.

    The point about using a functor is a good one, though, especially considering STL already provides such a functor: std::equal_to<T>
    - Alon

  5. #5
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: function template

    Quote Originally Posted by Hermit
    I can't imagine the purpose of a default template argument in this case anyway, since the type appears in the parameter list of the function and can be automatically deduced.

    The point about using a functor is a good one, though, especially considering STL already provides such a functor: std::equal_to<T>
    That's correct. I was just considering that it was for illustration purpose as an example of how to write a function template taking a type argument.

  6. #6
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: function template

    Quote Originally Posted by exterminator
    You can't have default template arguments with "function" templates. Making that a functor might solve the problem though.
    Yah, that is what I get for copy and paste without thinking
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  7. #7
    Join Date
    Mar 2008
    Posts
    55

    Re: function template

    well i get an error saying that its only allowed using a class template. is the only way to use a template is thru a class?
    also would that template work for both char and doubles?
    or am i getting that error because i havent called the template in the main?
    Code:
    #include<iostream>
    using namespace std;
    
    template <typename T = int>
    bool AreEqual(const T arg1, const T arg2){
    	return arg1==arg2;
    }
    
    int main(){
    	int a;
    	int b;
    
    	cout<<"enter two interger values: ";
    	cin>>a>>b;
    	cout<<a<<"and"<<b<<"are"
    		<<(             ?"equal":"not equal")<<'\n';
    
    	char c;
    	char d;
    
    	cout<<"\nenter two character values: ";
    	cin>>c>>d;
    	cout<<c<<"and"<<d<<"are"
    		<<(               ?"equal":"not equal")<<'\n';
    
    	double e;
    	double f;
    
    	cout<<"\nenter two double values: ";
    	cin>>e>>f;
    	cout<<e<<"and"<<f<<"are"
    		<<(               ?"equal":"not equal")<<'\n';
    	return 0;
    }
    Last edited by ptg; April 15th, 2008 at 06:23 PM.

  8. #8
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: function template

    Quote Originally Posted by souldog
    Yah, that is what I get for copy and paste without thinking
    Good to see you, one of the old-timers, back!
    Last edited by exterminator; April 16th, 2008 at 12:57 AM.

  9. #9
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: function template

    Code:
    template <typename T = int>
    bool AreEqual(const T arg1, const T arg2){
    	return arg1==arg2;
    }
    change above to:
    Code:
    template <typename T>
    bool AreEqual(const T& arg1, const T& arg2){ //change to reference if complex types might be compared as well
    	return arg1==arg2;
    }
    Or you could simply use std::equal_to<T> as suggested by Hermit. Follow the link he posted.

  10. #10
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: function template

    Quote Originally Posted by exterminator
    Good to see you, one of the old-timers, back!
    Thanks. Nice to be back
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured