Like Tree1Likes
  • 1 Post By brewbuck

Is there a way to restrict template arguments ?

This is a discussion on Is there a way to restrict template arguments ? within the C++ Programming forums, part of the General Programming Boards category; i.e... In this case.. can I restrict the template arguments to function pointers/objects ? Code: template<typename F1,typename F2> void compare(F1 ...

  1. #1
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498

    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 ?
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    iMalc likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



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, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21