can i create 1 template that i use inside the class and not when i create the class object?

Do you mean something 'ugly' like this?

#include <iostream>

template <class T>
class me
{

public:

    me():t(0) {}

    void create_T_pointer(const T & v) { t = new T(v); }

    const T* get_data() const { return  t; }

private:

    T * t;

};

template <class T>
std::ostream& operator <<(std::ostream & out, const me<T> & m )
{
    if ( m.get_data() )
        return out << *m.get_data();
    else
        return out << "Null pointer" << std::endl;
}

int main(int argc, char** argv)
{
    me<int> you;

    std::cout << you << std::endl;

    you.create_T_pointer(1234);

    std::cout << you << std::endl;

    return 0;
}

Edited 2 Years Ago by gerard4143

i mean like these:

template<typename T>
class variant2
{
private:
    void *Vvariant=NULL;
    T typ;
public:

    template<typename b>
    variant2 (b &value)
    {
        Vvariant=&value ;
        typ=b;

    }

    template<typename b>
    variant2 & operator = (b &value)
    {
        typ=b;
        Vvariant=&value ;

        return *this;
    }

    friend ostream& operator <<(ostream &os,const variant2 &obj)
    {
        os <<*static_cast<typ*>( obj.Vvariant) ;//see the typ
        return os;
    }
};

int main()
{
   variant2 d;
   d=100;
   cout << d;
}

but i get several errors :(

The problems with your code are not about having template members inside a class, but they are about pretty much everything else. I understand you "get several errors", but I can tell you that you are probably getting far too few errors, because there are many errors your compiler cannot diagnose, but I can ;) So, let me play the role of a "smart" compiler:

Line 10: Warning: A single-parameter constructor not marked as 'explicit' is an implicit conversion operator, it could trigger unintended conversions.

Line 10: Warning: Template argument b could be a const type.

Line 10: Warning: If b is a const type, the reference parameter value could be bound to a temporary or rvalue.

Line 12: Warning: Implicit cast to void*. Prefer to use explicit C++ casting operators, in this case, static_cast should be used.

Line 12: Error: If b is a const type, cannot convert b* to void* as it would cast away the cv-qualifiers (const).

Line 12: Error: Should not keep a persistent pointer to a passed-by-reference parameter that might be bound to an rvalue.

Line 13: Error: 'b' is a type identifier, not a value or a variable identifier.

Line 13: Error: No possible conversion from type identifier 'b' to variable 'typ' of type 'T'.

Line 18: Warning: Template argument 'b' could be a 'const' type.

Line 18: Warning: If 'b' is a 'const' type, the reference parameter 'value' could be bound to a temporary or rvalue.

Line 20: Error: 'b' is a type identifier, not a value or a variable identifier.

Line 20: Error: No possible conversion from type identifier b to variable typ of type T.

Line 21: Warning: Implicit cast to void*. Prefer to use explicit C++ casting operators, in this case, static_cast should be used.

Line 21: Error: If b is a const type, cannot convert b* to void* as it would cast away the cv-qualifiers (const).

Line 21: Error: Should not keep a persistent pointer to a passed-by-reference parameter that might be bound to an rvalue.

Line 28: Error: static_cast expects a type identifier as the destination type for the cast, instead, got typ*.

Line 28: Error: Expression typ* is incomplete or ill-formed. There is no such thing as a unary * operator after a variable or value.

Line 31: Warning: Class variant2 contains pointer data members but does not define an explicit destructor, copy-constructor, and copy-assignment operator.

Line 35: Error: Class variant2 is a template.

Line 35: Error: Class template variant2 takes 1 argument, 0 provided.

Line 36: Warning: Passing an rvalue bound to a const-reference to a function template expecting a reference parameter. (see messages from lines 18 to 21)

I think that these errors and warning messages are clear enough and cover pretty much every issue I can see with your code. I suggest your read them very carefully and make sure you understand every single piece of vocabulary in there and that you each of these errors or warnings and why they matter. Once you understand all these issues, I can guarantee you that you will understand all that is wrong with your code. And at that point, you will probably give up on that particular code and ask about how to solve the broader problem (variant class). But I don't want to go there until I'm sure you fully understand every single issue listed above.

Edited 2 Years Ago by mike_2000_17: re-order

This article has been dead for over six months. Start a new discussion instead.