Saturday, July 6, 2013

Specialization of a free standing template function

Newsgroup: comp.lang.c++

Subject: Specialization of a free standing template function

From: Daniel <danielaparker@...>

Date: Fri, 5 Jul 2013 18:26:24 -0700 (PDT)



Using Visual Studio v. 10



In test.hpp:



#ifndef TEST_HPP

#define TEST_HPP



#include <ostream>



namespace ns

{

template <class T>

void serialize(std::ostream& os, const T& A)

{

os << "null";

}



template <class T>

class A

{

public:

A(const T& A)

: A_(A)

{

}

void serialize(std::ostream& os) const

{

ns::serialize(os,A_);

}



T A_;

};

}

#endif



In test.cpp:



#include "test.hpp"



namespace ns

{

template <>

void serialize(std::ostream& os, const double& A)

{

os << A;

}

}



int main()

{

ns::A<double> a(10.0);

a.serialize(std::cout);



ns::B<double> a(10.0); // *

}



This compiles and links, and the output is



10



(as expected.)



However, if I include a second cpp file in the build, and move line *

to a function in that file, I get a linker error



serialize ... already defined in test.obj



Am I doing something wrong?



Thanks,

Daniel







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://www.pocketbinaries.com/usenet-forums/showthread.php?36618-Specialization-of-a-free-standing-template-function&goto=newpost

View all the progranning help forums at:

http://www.pocketbinaries.com/usenet-forums/forumdisplay.php?128-Coding-forums

No comments:

Post a Comment