Quantcast
Channel: Intel® C++ Compiler
Viewing all articles
Browse latest Browse all 1616

C++ templates program compiles on linux but not on windows

$
0
0

Hello,

the following example code uses templates and user defined conversion. It compiles and run correctly on linux using Intel Composer XE 2013 :

    template <typename TYPE>
    class Adult {
    public :
    	Adult(TYPE years): age(years) {};

    	TYPE getAge() const {
    		return age;
    	}

    private :
    	TYPE age;
    };

    template <typename TYPE>
    class Child {
    public :
    	Child(TYPE years): age(years) {};

    	TYPE getAge() const {
    		return age;
    	}

    private :
    	TYPE age;
    };

	template <typename TYPE, template <typename T> class PERSON=Child >
    class Human: public PERSON<TYPE> {
    public:
    	Human(TYPE years): PERSON<TYPE>(years){};

        operator Human<TYPE, Child>() const {
		    Human<TYPE, Child> res(this->getAge());
		   return res;
		};

    	TYPE getBirth(TYPE current) const {
    		return (current - this->getAge());
    	}
    };



int test() {

	Human<int,Adult> daddy(30);

	Human<int,Child> toto = daddy; // error : type "Child<TYPE>::Child [with TYPE=int]" is not a class template

	int birthDate = toto.getBirth(2014);

	return birthDate;
}


int main() {
	std::cout << "\n--[Athena,"<< test() << "] "; std::cout.flush();
}

On Windows, with the same compiler, an error occurs :

test.cpp(43): error : type "Child<TYPE>::Child [with TYPE=int]" is not a class template
test.cpp(43): error : type "Child<TYPE>::Child [with TYPE=int]" is not a class template
            operator Human<TYPE, Child>() const {
            operator Human<TYPE, Child>() const {
                                 ^
            detected during instantiation of class "Human<TYPE, PERSON> [with TYPE=int, PERSON=Child]" at line 59
                                 ^
            detected during instantiation of class "Human<TYPE, PERSON> [with TYPE=int, PERSON=Child]" at line 59

 

Is there any restriction on template use on windows ? Or does the compiler need some option enabled ?

 

AdjuntoTamaño
Descargartest.cpp1.29 KB

Viewing all articles
Browse latest Browse all 1616

Trending Articles