CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Tryong to understand templates

    I'm investigating a problem here when using boost::intrusive::list with templates but to get me started I wrote this small app (which works):-

    Code:
    #include <boost/intrusive/list.hpp>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    struct animal : public boost::intrusive::list_base_hook<>
    {
        string name;
        int legs;
        animal (string n, int l) : name{move(n)}, legs{l} {}
    };
    
    typedef boost::intrusive::list<animal> animal_list;
    
    void print_list (animal_list &ani)
    {
        for (const animal &a : ani)
            cout << " A " << a.name << " has " << a.legs << " legs" << endl;
    }
    
    int main()
    {
        animal a1{"dog", 4};
        animal a2{"spider", 6};
        animal a3{"budgie", 2};
     
        animal_list animals;
    
        animals.push_back(a1);
        animals.push_back(a2);
        animals.push_back(a3);
    
        print_list (animals);
    
        return 0;
    }
    Let's suppose I wanted to print out something else with legs - e.g. furniture. This sounds like the kinda thing templates are useful for. So how would I modify the code? e.g

    Code:
    struct furniture : public boost::intrusive::list_base_hook<>
    {
        string name;
        int legs;
        furniture (string n, int l) : name{move(n)}, legs{l} {}
    };
    
    typedef boost::intrusive::list<furniture> furniture_list;
    
    void print_list (animal_list &ani)  // <--- Using templates, how would I modify this to accept either furnitures or animals ??
    {
        // [[ What would be needed here ?? ]]
    }
    
    int main()
    {
        animal a1{"dog", 4};
        animal a2{"spider", 6};
        animal a3{"budgie", 2};
     
        animal_list animals;
    
        animals.push_back(a1);
        animals.push_back(a2);
        animals.push_back(a3);
    
        furniture f1{"table", 4};
        furniture f2{"chair", 4};
    
        furniture_list furnitures;
    
        furnitures.push_back(f1);
        furnitures.push_back(f2);
    
        print_list ([[ ?? ]]);  // <--- would need to be callable with either furnitures or animals
    
        return 0;
    }
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Tryong to understand templates

    Without using boost, do you mean something like this:

    Code:
    #include <list>
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    struct animal
    {
    	string name;
    	int legs {};
    	animal(string n, int l) : name {move(n)}, legs {l} {}
    };
    
    using animal_list = list<animal>;
    
    struct furniture
    {
    	string name;
    	int legs {};
    	furniture(string n, int l) : name {move(n)}, legs {l} {}
    };
    
    using furniture_list = list<furniture>;
    
    template<typename T>
    void print_list(const list<T>& elem)
    {
    	for (const T& a : elem)
    		cout << "A " << a.name << " has " << a.legs << " legs" << '\n';
    }
    
    int main()
    {
    	const animal a1 {"dog", 4};
    	const animal a2 {"spider", 6};
    	const animal a3 {"budgie", 2};
    
    	animal_list animals;
    
    	animals.push_back(a1);
    	animals.push_back(a2);
    	animals.push_back(a3);
    
    	const furniture f1 {"table", 4};
    	const furniture f2 {"chair", 4};
    
    	furniture_list furnitures;
    
    	furnitures.push_back(f1);
    	furnitures.push_back(f2);
    
    	print_list(animals);
    	print_list(furnitures);
    }
    Code:
    A dog has 4 legs
    A spider has 6 legs
    A budgie has 2 legs
    A table has 4 legs
    A chair has 4 legs
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Tryong to understand templates

    That looks promising 2kaud, thanks. I'll try adapting it here for boost.
    "A problem well stated is a problem half solved.” - Charles F. Kettering

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