Date: Thu, 28 Mar 2024 14:33:12 +0000 (UTC) Message-ID: <343559823.20109.1711636397424@confluence> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_20108_1257444708.1711636392416" ------=_Part_20108_1257444708.1711636392416 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
# -*- pyt= hon -*- # # Setup our environment # import lsst.SConsUtils as scons env =3D scons.makeEnv("Foo", r"$HeadURL$", []) env.SharedLibrary('Foo', "Foo.cc") env.Program(["main.cc"], LIBS=3D"Foo", LIBPATH=3D".")
#if !def= ined(FOO_H) #define FOO_H 1 #include <cstddef> template<typename T> class Foo { public: explicit Foo(); explicit Foo(T x); ~Foo(); inline T getVal() const; void setVal(T val); private: T _val; }; // // Inlined functions need to be in the .h file (maybe in the class definiti= on itself) // template<typename T> inline T Foo<T>::getVal() const { return _val; } // // Declare a templated function // template<typename T> size_t mySizeof(void); // // Provide a declaration for explicitly-instantiated version[s] of mySizeof= () // This is only needed if we provide enough information here for the compil= er // to instantiate the function/class // // It's non-compliant code (but see C++ standards proposal N1897), and stop= s // the compiler instantiating the function in each file, and leaving it up // to the linker to clean up the mess // #if 0 extern template size_t mySizeof<float>(void); #endif #endif
//= / Implementation of templated class Foo #include "Foo.h" template<typename T> Foo<T>::Foo() : _val(0) {} template<typename T> Foo<T>::Foo(T val) : _val(val) {} template<typename T> Foo<T>::~Foo() {} #if 0 template<typename T> T Foo<T>::getVal() const; // inline in Foo= .h #endif template<typename T> void Foo<T>::setVal(T val) { _val =3D val; } // // And a templated function // template<typename T> size_t mySizeof() { return sizeof(T); } // // Explicit instantiations // template class Foo<float>; template class Foo<int>; template size_t mySizeof<float>();
#i= nclude <iostream> #include "Foo.h" int main() { Foo<int> i; Foo<float> x(1.234); std::cout << "i =3D " << i.getVal() << ", " << = x.getVal() << std::endl; =20 i.setVal(10); std::cout << "i =3D " << i.getVal() << std::endl; std::cout << "mySizeof(float) =3D " << mySizeof<float>= ;() << std::endl; }