Thread: Is there a way to restrict template arguments ?

  1. #1
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657

    Is there a way to restrict template arguments ?

    i.e... In this case.. can I restrict the template arguments to function pointers/objects ?
    Code:
        template<typename F1,typename F2>
        void compare(F1 fp1,F2 fp2)
        {
            double t1,t2;
            clock_t ini = clock();
            fp1();
            clock_t fin = clock();
            t1 = double((fin - ini))/CLOCKS_PER_SEC;
            ini = clock();
            fp2();
            fin = clock();
            t2 = double((fin - ini))/CLOCKS_PER_SEC;        
            std::cout<<"First one took: "<<t1<<" seconds\n";
            std::cout<<"Second one took: "<<t2<<" seconds\n";
        }
    Last edited by manasij7479; 10-19-2011 at 08:02 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You don't want to allow function objects?

    Anyway, you are effectively restricting it to things that appear to be callable. If you want more than that, you need the concepts thing.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by laserlight View Post
    You don't want to allow function objects?

    Anyway, you are effectively restricting it to things that appear to be callable. If you want more than that, you need the concepts thing.
    No, I don't want to restrict function objects, just the other types of arguments..
    They show an error message, and I posted before checking that.

    Anyway.. what are "Concepts thing"s ?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by manasij7479
    Anyway.. what are "Concepts thing"s ?
    A feature that was supposed to be included in the current version of C++, but then got ditched because the standard committee felt that it should be improved first. The Boost Concept Check Library is related to this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by manasij7479 View Post
    i.e... In this case.. can I restrict the template arguments to function pointers/objects ?
    Since the code uses the arguments as if they were callable, if you pass something that isn't callable the thing won't compile, so I'm not sure what you are trying to prevent.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by brewbuck View Post
    Since the code uses the arguments as if they were callable, if you pass something that isn't callable the thing won't compile, so I'm not sure what you are trying to prevent.
    I realized that just after posting this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mixing implicit and explicit template arguments
    By Mozza314 in forum C++ Programming
    Replies: 7
    Last Post: 01-21-2011, 06:44 PM
  2. explanation regarding Template arguments
    By babu198649 in forum C++ Programming
    Replies: 8
    Last Post: 09-09-2008, 05:22 AM
  3. How to handle illegal template arguments?
    By TriKri in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2008, 12:06 PM
  4. Deducing Function Template Arguments
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2008, 07:29 PM
  5. Template arguments
    By Kaiser in forum C++ Programming
    Replies: 5
    Last Post: 10-10-2006, 04:29 PM