Default template arguments in function templates

Discussion in 'C++' started by George Sakkis, Oct 4, 2005.

  1. Hi all,

    I have the following two template function definitions:

    template <typename Container, typename Sepatator>
    string join(const Container& c, const Sepatator& s) {
    // ...
    }

    template <typename Container>
    string join(const Container& c) {
    return join(c, ' ');
    }

    I tried to merge them into one using a default argument for Separator, but the compiler (gcc 3.3.1)
    complains that default template arguments may not be used in function templates. What's the reason
    for this limitation and, more importantly, is there a workaround for it ?

    TIA,
    George
    George Sakkis, Oct 4, 2005
    #1
    1. Advertising

  2. George Sakkis wrote:
    > I have the following two template function definitions:
    >
    > template <typename Container, typename Sepatator>
    > string join(const Container& c, const Sepatator& s) {
    > // ...
    > }
    >
    > template <typename Container>
    > string join(const Container& c) {
    > return join(c, ' ');
    > }
    >
    > I tried to merge them into one using a default argument for Separator, but the compiler (gcc 3.3.1)
    > complains that default template arguments may not be used in function templates. What's the reason
    > for this limitation and, more importantly, is there a workaround for it ?


    Ask in comp.std.c++ bout the reasoning behind prohibiting the default
    template arguments for function templates, they discuss the actual
    Standard document, they know the rationales for different parts of it
    for sure. As to the work-around, you already have it: don't merge, keep
    the two functions separate.

    V
    Victor Bazarov, Oct 4, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page