Thread: Issue with function pointer and template

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    112

    Issue with function pointer and template

    The template at the top is giving me an error when I go to compile using the sort function. My guess is the compiler needs to know what data types are going to be used at compile time.

    Is there a workaround?

    Code:
    // VectorApp1.cpp : Defines the entry point for the console application.       //
    
    
    #include "stdafx.h"
    #include <iostream>
    #include <vector>
    #include <cstdlib>
    #include <ctime>
    #include <algorithm>
    
    
    //template <typename lhs, typename rhs>
    //bool isLhsLessRhsFunc(lhs, rhs) { return (lhs<rhs); }
    
    
    bool isLhsLessRhsFunc(int lhs, int rhs) { return (lhs<rhs); }
    
    
    int main()
    {
    	std::vector<int> testVec;
    	
    	std::cout << "Size of vec: " << testVec.size() << std::endl;
    
    
    	std::srand(std::time(0)); // use current time as seed for random generator
    
    
    	for (int i = 0; i < 10000; i++)
    	{
    		testVec.push_back(std::rand());
    	}
    
    
    	for (auto it = testVec.begin(); it != testVec.end(); ++it)
    	{
    		std::cout << *it << '\n';
    	}
    
    
    	std::cout << "Size of vec: " << testVec.size() << std::endl;
    
    
    	std::sort(testVec.begin(), testVec.end(), isLhsLessRhsFunc);
    
    
    	for (auto it = testVec.begin(); it != testVec.end(); ++it)
    	{
    		std::cout << *it << '\n';
    	}
    
    
        return 0;
    }

  2. #2
    Guest
    Guest
    The issue with your function template is twofold. lhs and rhs represent types in your template, but you're using them like variables, i.e. bool f(int, int) { return (int < int); }

    The other issue is that you need tell the compiler how to instantiate the function on line 44, e.g. isLhsLessRhsFunc<int, int> for integers. Since both arguments presumably share the same type, you might want to use a single template parameter. I assume you're doing this to learn about templates – otherwise std::less or a lambda are neater solutions imo.
    Last edited by Guest; 08-15-2016 at 09:53 PM. Reason: typo

  3. #3
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Adrian is right, this needs to be:
    Code:
    //template <typename lhs, typename rhs>
    //bool isLhsLessRhsFunc(lhs, rhs) { return (lhs<rhs); }
    
    template <typename T>
    bool isLessThan(T const& lhs, T const& rhs) { return lhs < rhs; };
    And much as Adrian already mentioned, I highly recommend just using:
    Code:
    std::sort(begin, end, std::less<T>{});
    so that you're not re-implementing what's already in the STL.
    Last edited by MutantJohn; 08-16-2016 at 10:27 AM.

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Thanks for the help, and I can get it to work like that, but I'm trying to get it to work more abstractly, if that is a word.

    templates - C++ Get Vector type - Stack Overflow

    They are using a template of a template and that would be pretty slick at least in my eyes, but I haven't been able to get it compile for me.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The syntax of a template of template is

    template<typename, ...> class T

    The typename, ... part is basically all the template arguments of your template. So in your case, you want

    template<typename, typename> class T

    Btw, you can also give them a name:

    template<typename A, typename B> class T

    Now A, B and T are all types which you can use in your function.

    However, instead of going this way, I'd go with C++14's generic lambdas:

    Code:
    std::sort(testVec.begin(), testVec.end(), [](auto l, auto r) { return (l < r); });
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Trying to be too clever has been the downfall of many great programmers.... I'm assuming.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    112
    Ok, I used some of functions in the vector container library, a lambda function and the std::less function. The replies have been helpful.

    There is a lot of syntax to learn in C++, that's for sure.

    Code:
    #include <vector>#include <cstdlib>
    #include <ctime>
    #include <algorithm>
    
    
    auto isLhsLessRhsFunc = [](auto a, auto b) { return a < b; };
    
    
    template <typename T>
    void printContainer(T a) 
    { 
    	for(auto it = a.begin(); it != a.end(); ++it)
    	{
    		std::cout << *it << '\n';
    	}
    }
    
    
    int main()
    {
    	std::vector<int> testVec(10);
    	std::vector<int> testVecToSwap{5};
    		
    	std::cout << "Size of vec: " << testVec.size() << std::endl;
    
    
    	std::srand(std::time(0)); // use current time as seed for random generator
    	
    	for (auto it = testVec.begin(); it != testVec.end(); ++it)
    	{
    		*it = std::rand();
    	}
    
    
    	std::cout << "Vec Original content" << std::endl;
    	printContainer(testVec);
    
    
    	std::cout << "Size of vec: " << testVec.size() << std::endl;
    	std::cout << "Max Size of vec: " << testVec.max_size() << std::endl;
    	std::cout << "Capacity of vec: " << testVec.capacity() << std::endl;
    
    
    	std::sort(testVec.begin(), testVec.end(), isLhsLessRhsFunc);
    
    
    	std::cout << "Vec Sorted content" << std::endl;
    	printContainer(testVec);
    
    
    	std::random_shuffle(testVec.begin(), testVec.end());
    
    
    	std::cout << "Vec Random content" << std::endl;
    	printContainer(testVec);
    
    
    	std::sort(testVec.begin(), testVec.end(), std::less<>());
    	std::cout << "Vec Sorted content" << std::endl;
    	printContainer(testVec);
    
    
    	testVec.insert(testVec.begin(), 1);
    
    
    	std::cout << "Vec Inserted content" << std::endl;
    	printContainer(testVec);
    
    
    	testVec.erase(testVec.begin());
    
    
    	std::cout << "Vec Erased content" << std::endl;
    	printContainer(testVec);
    
    
    	testVec.pop_back();
    	std::cout << "Vec popback" << std::endl;
    	printContainer(testVec);
    
    
    	testVec.swap(testVecToSwap);
    	std::cout << "Vec swap: testVec" << std::endl;
    	printContainer(testVec);
    
    
    	std::cout << "Vec swap: testVecToSwap" << std::endl;
    	printContainer(testVecToSwap);
    
    
    	testVec.erase(testVec.begin(), testVec.end());
    	std::cout << "Vec All Erased content" << std::endl;
    	printContainer(testVec);
    	
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-08-2015, 04:39 AM
  2. Template function pointer???
    By NorthBlast in forum C++ Programming
    Replies: 4
    Last Post: 02-10-2014, 04:34 PM
  3. Pointer to template function workaround
    By VirtualAce in forum C++ Programming
    Replies: 9
    Last Post: 01-13-2012, 04:18 PM
  4. function pointer issue
    By drshmoo in forum C Programming
    Replies: 7
    Last Post: 09-22-2011, 03:04 PM
  5. template function issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 12-12-2007, 12:39 PM

Tags for this Thread