Syntax for Template Member Functions

Consider the following code:

template <typename Datatype>
class MyClass
{
    void doStuff();

    template <typename AnotherDatatype>
    void doTemplateStuff(AnotherDatatype Argument);
};

template <typename Datatype>
void MyClass<Datatype>::doStuff()
{
    // ...
}

template <typename Datatype>
template <typename AnotherDatatype>
void MyClass<Datatype>::doTemplateStuff(AnotherDatatype Argument)
{
    // ...
}

The implementation for the second member function doTemplateStuffwill not compile if I condense it like this:

template <typename Datatype, typename AnotherDatatype>
void MyClass<Datatype>::doTemplateStuff(AnotherDatatype Argument)
{
    // ...
}

Why is this? Shouldn't the template information be separated by commas, just like putting each typenameon its own line? Or is there some subtle difference that I don’t know about ...?

(Also, if someone might think of a better headline, please let me know.)

+3
source share
2 answers

. , , - . , , , , , . :

& ; . & Lambda; . x + y

( )

& lambda; (x, y). x + y

(x, y) - , x y.

++ ++ . , . , ++ .

, . , - . - , .

+3

. , , , , . MyClass, , .

,

template<typename T, typename V>
struct Foo{
    void bar();
};

template<typename T, typename V>
void Foo<T,V>::bar(){...}

- , .

template<typename T>
struct Foo{
    void bar();
}

template<typename T, typename V>
void Foo<T>::bar(){...}

- . , .

, :

template<typename T>
struct Foo{
    template<typename V>
    void bar(const V& _anInputValue){
        cout << _anInputValue;
    }

    void baz();
};

template<typename T>
void Foo<T>::baz(){
    cout << "Another function.";
}
+2

Source: https://habr.com/ru/post/1786297/


All Articles