$include_dir="/home/hyper-archives/boost-users/include"; include("$include_dir/msg-header.inc") ?>
From: Sliwa, Przemyslaw (London) (Przemyslaw_Sliwa_at_[hidden])
Date: 2005-05-12 05:32:27
Thanks for help,
I have an additional question. Does anyone know why the following
program gives different output on VC++ 6 and VC++ 2003 NET? It is pretty
strange, isn't it?
Thanks in advance.
Pshemek
#include <iostream>
#include <cmath>
using namespace std;
class A
{	
        protected:
                char m_d;
};
class B : public A
{
        public:
                typedef char s;
        private:
                unsigned short g;
};
class C : public A
{
        public:
                typedef short unsigned s;
        private:
                unsigned d;
};
template<bool cond>
class Select {};
template<> class Select<true>
{
        static void Statment1(double g)
        {
                cout << g*g << endl;
        }
        public:
                static void f(double g)
                {
                        Statment1(g);
                }
};
template<> class Select<false>
{
        static void Statment2(double g)
        {
                cout << sqrt(g) << endl;
        }
        public:
                static void f(double g)
                {
                        Statment2(g);
                }
};
template<bool cond> void exec()
{
        Select<cond>::f(3.0);
}
template<class T>
void foo()
{
        exec<sizeof(T::s)==sizeof(B::s)>();
        exec<sizeof(T::s)==sizeof(C::s)>();	
}
int main()
{
        foo<B>();
        foo<C>();
        return 0;
}
-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Eric Niebler
Sent: 11 May 2005 18:37
To: boost-users_at_[hidden]
Subject: [Boost-users] Re: Question
Sliwa, Przemyslaw (London) wrote:
> 
> I would like to write a template which depending on the argument type 
> does different things.
>  
> For example the pseudo code:
>  
> template <class T, class C>
> foo(T& nT, C& nC)
> {
>     // common code;
>     if(T==C)
>     // specific code;
>     .....
>  
>     // again common code;
> }
>  
> Can the template meta programming help me in this case?
It may. It depends on whether "specific code" compiles when T!=C. If it 
does, you could do this:
template <class T, class C>
void foo(T& nT, C& nC)
{
     // common code
     if(boost::is_same<T,C>::value)
       { /* specific code */ }
     // again common code
}
where is is_same<> is defined in boost/type_traits/is_same.hpp. If 
"specific code" doesn't compile when T!=C, you need to get fancier. An 
overloaded helper function is the way to go:
template<class T, class C>
void specific_code(T&, C&) {}
template<class T>
void specific_code(T&, T&)
{
   // specific code here
}
template <class T, class C>
void foo(T& nT, C& nC)
{
     // common code
     specific_code(nT, nC);
     // again common code
}
HTH,
-- Eric Niebler Boost Consulting www.boost-consulting.com _______________________________________________ Boost-users mailing list Boost-users_at_[hidden] http://listarchives.boost.org/mailman/listinfo.cgi/boost-users -------------------------------------------------------- If you are not an intended recipient of this e-mail, please notify the sender, delete it and do not read, act upon, print, disclose, copy, retain or redistribute it. Click here for important additional terms relating to this e-mail. http://www.ml.com/email_terms/ --------------------------------------------------------