explicit templates instantiation .... more

Discussion in 'C++' started by ma740988@pegasus.cc.ucf.edu, Jul 13, 2005.

  1. Guest

    ///// 1
    Consider the _FUNCTION_ template

    template <typename T>
    int spaceOf ()
    {
    int bytes = sizeof T;
    return bytes / 4
    + bytes % 4 > 0;
    }
    Since C++ templates requires more intelligence from the environment
    'we' have to tell the compiler about the actualy type:
    So now:
    class C {};
    typdef void (*ptrFun)(int);

    int idx = spaceOf<C>();
    int jdx = spaceOf<ptrFunc>();

    Except how do i achieve the same feat here for this fuction template:

    class D {
    public:
    template<typename T> // here we go again....
    D( T& t, void(T::*f)() )
    {}
    };

    class UseD {
    void someSpecialFunc() {};
    D *ptrD;
    public:
    UseD() {
    ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);
    // validate
    }
    };

    ////////////// 2
    For starters this is a 'C' ism and I suspect (scanned the ISO C++
    standard and couldn't find anything) both are legal (compiler didn't
    complain)?

    a. typedef _MYSTRUCT {} MYSTRUCT;
    b. typedef MYSTRUCT {} MYSTRUCT;

    b at first appeared 'troubling' to me when I first encounted this and -
    admittidely - I thought the compiler would complain.
    , Jul 13, 2005
    #1
    1. Advertising

  2. wrote:
    > ///// 1
    > Consider the _FUNCTION_ template
    >
    > template <typename T>
    > int spaceOf ()
    > {
    > int bytes = sizeof T;


    int bytes = sizeof(T);

    > return bytes / 4
    > + bytes % 4 > 0;
    > }
    > Since C++ templates requires more intelligence from the environment
    > 'we' have to tell the compiler about the actualy type:
    > So now:
    > class C {};
    > typdef void (*ptrFun)(int);
    >
    > int idx = spaceOf<C>();
    > int jdx = spaceOf<ptrFunc>();
    >
    > Except how do i achieve the same feat


    What feat is that?

    > here for this fuction template:
    >
    > class D {
    > public:
    > template<typename T> // here we go again....
    > D( T& t, void(T::*f)() )
    > {}


    Are you looking to make use of 'T' here? Or of 't'? Or of 'f'? What
    seems to be the problem?

    > };
    >
    > class UseD {
    > void someSpecialFunc() {};
    > D *ptrD;
    > public:
    > UseD() {
    > ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);
    > // validate
    > }
    > };


    Since I don't know what "feat" it is, I am not sure what to tell you.

    >
    > ////////////// 2
    > For starters this is a 'C' ism and I suspect (scanned the ISO C++
    > standard and couldn't find anything) both are legal (compiler didn't
    > complain)?


    Is there a question here somewhere?

    > a. typedef _MYSTRUCT {} MYSTRUCT;
    > b. typedef MYSTRUCT {} MYSTRUCT;


    Did you mean

    typedef struct BLAH {} BLAHBLAH;

    ? Without the keyword 'struct', it's _illegal_.

    > b at first appeared 'troubling' to me when I first encounted this and -
    > admittidely - I thought the compiler would complain.


    I feel your confusion.

    V
    Victor Bazarov, Jul 13, 2005
    #2
    1. Advertising

  3. > Except how do i achieve the same feat here for this fuction template:
    >
    > class D {
    > public:
    > template<typename T> // here we go again....
    > D( T& t, void(T::*f)() )
    > {}
    >
    >};
    >
    > class UseD {
    > void someSpecialFunc() {};
    > D *ptrD;
    > public:
    > UseD() {
    > ptrD = new (std::nothrow) D(*this, &UseD::someSpecialFunc);


    You can't. 14.8.1.4 in 1997 draft standard.

    > // validate
    > }
    >
    > };
    >
    > ////////////// 2
    > For starters this is a 'C' ism and I suspect (scanned the ISO C++
    > standard and couldn't find anything) both are legal (compiler didn't
    > complain)?
    >
    > a. typedef _MYSTRUCT {} MYSTRUCT;
    > b. typedef MYSTRUCT {} MYSTRUCT;


    I think you mean

    a. typedef struct _MYSTRUCT {} MYSTRUCT;
    b. typedef struct MYSTRUCT {} MYSTRUCT;

    > b at first appeared 'troubling' to me when I first encounted this and -
    > admittidely - I thought the compiler would complain.


    Struct names live in a different 'namespace', that's a relic from C.
    Don't do that:

    struct MYSTRUCT
    {
    };

    is enough in C++.


    Jonathan
    Jonathan Mcdougall, Jul 13, 2005
    #3
  4. Guest

    Victor, how are you :)

    || Are you looking to make use of 'T' here? Or of 't'? Or of 'f'?
    What seems to be the problem?

    I have to explicitly initialize T. Couldn't quite figure out how to
    get there 'in' this case. The template functoin spaceOf was trivial in
    comparison.

    || ? Without the keyword 'struct', it's _illegal_.
    The distiction between a and b lies in the underscore for the type
    MYSTRUCT. I was perusing souce code and saw a mix of

    typedef _ST1 {} ST1 ; // note the underscore for the type
    typedef ST2 {} ST2; // not the same here.

    I thought to myself WTH. It was - apparently - deliberate. One or
    two instance and I'd assume 'typo' but given 12/13 structs with 6/7
    with underscore and the rest withouth, I became curious. I just prefer
    consistency but first I thought there was something I was missing
    , Jul 13, 2005
    #4
  5. Guest

    | I think you mean

    | a. typedef struct _MYSTRUCT {} MYSTRUCT;
    | b. typedef struct MYSTRUCT {} MYSTRUCT;

    Yes, I'm so sorry... Crud I can't belive I missed the keyword. I was
    so hung up on the underscore that I mistyped.
    , Jul 14, 2005
    #5
  6. wrote:
    > Victor, how are you :)


    Thanks, I am fine. How are you?

    >>> Are you looking to make use of 'T' here? Or of 't'? Or of 'f'?
    >>> What seems to be the problem?

    >
    > I have to explicitly initialize T.


    I don't understand this statement. In a templated constructor you
    cannot use explicit template argument syntax, you _have_to_ rely on
    the template argument deduction from the function argument:

    #include <typeinfo>
    #include <iostream>
    struct Foo {
    template<class T> Foo(T t) {
    std::cout << "Foo<" << typeid(T).name() << ">(" << t << ")\n";
    }
    };

    int main() {
    Foo foo(42);
    }

    (compile and run it, you should see something like)
    ---------
    Foo<int>(42)
    ---------

    > Couldn't quite figure out how to
    > get there 'in' this case. The template functoin spaceOf was trivial
    > in comparison.


    I don't understand why you'd need to explicitly specify the template
    argument when it can be easily deduced by the compiler.

    V
    Victor Bazarov, Jul 14, 2005
    #6
    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. Rolf Magnus
    Replies:
    0
    Views:
    844
    Rolf Magnus
    Jul 23, 2003
  2. C. Carbonera
    Replies:
    4
    Views:
    1,340
    C. Carbonera
    Feb 5, 2004
  3. Fernando Cuenca
    Replies:
    4
    Views:
    2,522
    Gianni Mariani
    Sep 6, 2004
  4. Steven T. Hatton
    Replies:
    3
    Views:
    369
    Craig Scott
    Dec 5, 2006
  5. Replies:
    1
    Views:
    571
    Salt_Peter
    Dec 25, 2006
Loading...

Share This Page