How to instantiate template when it is arguments satisfy some condition?

Discussion in 'C++' started by PengYu.UT@gmail.com, Nov 6, 2005.

  1. Guest

    For example, I want the return_type of the two arguments of Expr2 be
    the same. Otherwise, the compilor should give an error.

    Would you please tell me how to do that?

    Thanks,
    Peng


    template <typename T>
    struct Expr1{
    typedef T return_type;
    };

    template <typename E, typename T>
    struct Expr2;

    template <typename E1, typename E2>
    struct Expr2 {//I want E1 and E2's return_type be the same.
    typedef typename E1::return_type return_type;
    };

    int main(int argc, char *argv[]){
    Expr2<Expr1<int>, Expr1<double> > a;
    }
    , Nov 6, 2005
    #1
    1. Advertising

  2. * :
    > For example, I want the return_type of the two arguments of Expr2 be
    > the same. Otherwise, the compilor should give an error.
    >
    > Would you please tell me how to do that?


    template< typename A, typename B >
    struct IsSameType{ enum{ yes=false }; };

    template< typename T >
    struct IsSameType<T, T>{ enum{ yes=true }; };


    > template <typename T>
    > struct Expr1{
    > typedef T return_type;
    > };
    >
    > template <typename E, typename T>
    > struct Expr2;
    >
    > template <typename E1, typename E2>
    > struct Expr2 {//I want E1 and E2's return_type be the same.
    > typedef typename E1::return_type return_type;


    BOOST_STATIC_ASSERT((
    IsSameType<typename E1::return_type, typename E2::return_type>::yes
    ));

    > };
    >
    > int main(int argc, char *argv[]){
    > Expr2<Expr1<int>, Expr1<double> > a;
    > }




    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Nov 6, 2005
    #2
    1. Advertising

  3. Tao Wang Guest

    Re: How to instantiate template when it is arguments satisfy somecondition?

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    wrote:
    > For example, I want the return_type of the two arguments of Expr2 be
    > the same. Otherwise, the compilor should give an error.
    >
    > Would you please tell me how to do that?
    >
    > Thanks,
    > Peng
    >
    >
    > template <typename T>
    > struct Expr1{
    > typedef T return_type;
    > };
    >
    > template <typename E, typename T>
    > struct Expr2;
    >
    > template <typename E1, typename E2>
    > struct Expr2 {//I want E1 and E2's return_type be the same.
    > typedef typename E1::return_type return_type;
    > };
    >
    > int main(int argc, char *argv[]){
    > Expr2<Expr1<int>, Expr1<double> > a;
    > }
    >


    To avoid the problem, why don't use one type, if two type are actually same?

    template <typename T>
    struct Expr1{
    typedef T return_type;
    };

    template <typename E>
    struct Expr2 {
    typedef typename E::return_type return_type;
    };

    int main(int argc, char *argv[]){
    Expr2<Expr1<int> > a;
    return 0;
    }
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org

    iD8DBQFDbpjlRS5AkKgtcCcRAhsEAJ0TZEl0k6Z9U2mAOV2CzIo2MoNOQgCaA4kt
    Fg7JcU7n5qYwrRZOC734hc4=
    =agtk
    -----END PGP SIGNATURE-----
    Tao Wang, Nov 6, 2005
    #3
  4. Guest

    Tao Wang wrote:
    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > wrote:
    > > For example, I want the return_type of the two arguments of Expr2 be
    > > the same. Otherwise, the compilor should give an error.
    > >
    > > Would you please tell me how to do that?
    > >
    > > Thanks,
    > > Peng
    > >
    > >
    > > template <typename T>
    > > struct Expr1{
    > > typedef T return_type;
    > > };
    > >
    > > template <typename E, typename T>
    > > struct Expr2;
    > >
    > > template <typename E1, typename E2>
    > > struct Expr2 {//I want E1 and E2's return_type be the same.
    > > typedef typename E1::return_type return_type;
    > > };
    > >
    > > int main(int argc, char *argv[]){
    > > Expr2<Expr1<int>, Expr1<double> > a;
    > > }
    > >

    >
    > To avoid the problem, why don't use one type, if two type are actually same?
    >
    > template <typename T>
    > struct Expr1{
    > typedef T return_type;
    > };
    >
    > template <typename E>
    > struct Expr2 {
    > typedef typename E::return_type return_type;
    > };
    >
    > int main(int argc, char *argv[]){
    > Expr2<Expr1<int> > a;
    > return 0;
    > }

    Because I might have Expr3 which can be used as the template arguments
    of Expr2.

    template <typename T>
    struct Expr3{
    typedef std::complex<T> return_type;
    };
    , Nov 7, 2005
    #4
  5. Guest

    Alf P. Steinbach wrote:
    > * :
    > > For example, I want the return_type of the two arguments of Expr2 be
    > > the same. Otherwise, the compilor should give an error.
    > >
    > > Would you please tell me how to do that?

    >
    > template< typename A, typename B >
    > struct IsSameType{ enum{ yes=false }; };
    >
    > template< typename T >
    > struct IsSameType<T, T>{ enum{ yes=true }; };
    >
    >
    > > template <typename T>
    > > struct Expr1{
    > > typedef T return_type;
    > > };
    > >
    > > template <typename E, typename T>
    > > struct Expr2;
    > >
    > > template <typename E1, typename E2>
    > > struct Expr2 {//I want E1 and E2's return_type be the same.
    > > typedef typename E1::return_type return_type;

    >
    > BOOST_STATIC_ASSERT((
    > IsSameType<typename E1::return_type, typename E2::return_type>::yes
    > ));
    >
    > > };
    > >
    > > int main(int argc, char *argv[]){
    > > Expr2<Expr1<int>, Expr1<double> > a;
    > > }

    Do you know how BOOST_STATIC_ASSERT is implemented? I might not be able
    to use boost.

    Thanks,
    Peng
    , Nov 7, 2005
    #5
  6. wrote:

    > Do you know how BOOST_STATIC_ASSERT is implemented?


    Yes.

    Why don't you look at the source code for yourself? You can download
    it and use it, subject to Boost's license.

    http://www.boost.org/boost/static_assert.hpp

    You can also find a similar static assert macro in Modern C++ Design.

    Best regards,

    Tom
    Thomas Tutone, Nov 7, 2005
    #6
  7. John Carson Guest

    <> wrote in message
    news:
    > For example, I want the return_type of the two arguments of Expr2 be
    > the same. Otherwise, the compilor should give an error.
    >
    > Would you please tell me how to do that?
    >
    > Thanks,
    > Peng
    >
    >
    > template <typename T>
    > struct Expr1{
    > typedef T return_type;
    > };
    >
    > template <typename E, typename T>
    > struct Expr2;
    >
    > template <typename E1, typename E2>
    > struct Expr2 {//I want E1 and E2's return_type be the same.
    > typedef typename E1::return_type return_type;
    > };
    >
    > int main(int argc, char *argv[]){
    > Expr2<Expr1<int>, Expr1<double> > a;
    > }


    template <typename T>
    struct Expr1
    {
    typedef T return_type;
    };

    // forward declare only
    template<class A, class B>
    class EqualTypes;

    // specialize for A == B
    template<class A>
    class EqualTypes<A,A>
    {};

    template <typename E1, typename E2>
    struct Expr2
    {
    EqualTypes<typename E1::return_type, typename E2::return_type> eq;
    };

    int main()
    {
    Expr2<Expr1<int>, Expr1<double> > a;
    }


    --
    John Carson
    John Carson, Nov 7, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    676
    Remon van Vliet
    Jun 15, 2005
  2. Fei Liu
    Replies:
    0
    Views:
    412
    Fei Liu
    Oct 25, 2007
  3. Fei Liu
    Replies:
    4
    Views:
    753
    Victor Bazarov
    Oct 26, 2007
  4. Replies:
    2
    Views:
    327
  5. Bill W.
    Replies:
    13
    Views:
    279
    Phillip Gawlowski
    May 9, 2011
Loading...

Share This Page