Results 1 to 8 of 8

Thread: string class: Using template constructor

  1. #1

    Thread Starter
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196

    string class: Using template constructor

    I am using the string class in VC++ 6. I am attempting to use the template class constructor:

    template <class InIt>
    basic_string(InIt first, InIt last, const A& al = A());

    However, when I attempt to construct a string using this I get errors about how there are no overloaded versions acceptable - or that it cannot convert from iterator to whatever type it feels like!

    How can I use this templated class constructor?

    Thanks

    HD

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    You can only use basic_string for char or wchar_t, unfortunately...
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    Addicted Member HairyDave's Avatar
    Join Date
    Aug 2002
    Location
    Er...I can't remember.
    Posts
    196
    Hmm. I don't mean to question you, but the MSDN library documents a templated constructor that takes an initial point of an iterator, and a final point of an iterator (and the thing itself). It then 'should' give back a basic_string.

    Have you heard of this before and have found that it doesn't work? I'd like to know of any information you have on the subject.

    Although I've been using C and C++ for years now, I'm more of a C programmer and so the string class is a bit unfamiliar.

    Thanks for any information

    HD

  4. #4
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Unfortunatly VC6 doesn't support member template functions:
    MSDN:
    In this implementation, if a translator does not support member template functions, the template:
    Code:
    template <class InIt>
        basic_string(InIt first, InIt last, const A& al = A());
    is replaced by:
    Code:
    basic_string(const_iterator first, const_iterator last,
        const A& al = A());
    The only solutions I can think of are:
    1. Don't use VC6
    2. first construct an empty string, then use std::copy(first,last,out)

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by twanvl
    Unfortunatly VC6 doesn't support member template functions:


    The only solutions I can think of are:
    1. Don't use VC6
    2. first construct an empty string, then use std::copy(first,last,out)
    Unfortunatly the same restriction applies for std::copy.

    If you ever choose to use a good compiler (gcc, VC++7, in that order ) you would do for example

    list<wchar_t> ll;
    ll.push_back(L'H');
    ll.push_back(L'e');
    ll.push_back(L'l');
    ll.push_back(L'l');
    ll.push_back(L'o');

    basic_string<wchar_t> str(ll.begin(), ll.end());
    wcout << str << endl;

    which would output
    Hello

    You can only use containers that contain the same element type as the string, so if you use string ( = basic_string<char>) you can only copy from <char> containers. If you use wstring ( = basic_string<wchar_t>) you can only use <wchar_t> containers (like list<wchar_t>)
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    /me has something going at the back of his mind that you can use iterator adapters to do a locale-conversion from wchar_t to char inline
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Sounds good. Do it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    If I ever get time

    So far most of my programming-related projects are on hold while I try and get my (real) work together
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

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