Thread: template in c++

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up template in c++

    which is a valid friend declaration for class template given below. I guess line 2 is valid...any suggestions?

    Code:
    template<class T> class Y {};
    template<class T> class X{
    
    friend class T;  //line 1
    friend class Y <T>; //line 2
    friend T;    //line 3
    };
    
    int main () {
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are aware that, if you obtained a compiler, you could experiment with such questions, and learn more in the doing. Throwing random, undirected, and (mostly) trivial and pointless questions at a forum is an effective way to annoy other people by wasting their time, while minimising your chances of learning anything useful.

    You might also try explaining WHY you think line 2 is valid, but that lines 1 and 3 are not. The process of writing that description down will take more effort on your part, but you will learn more in the process, and annoy other people less when they see you applying genuine efforts to understand rather than asking increasingly pointless questions.

    Note that, with templates, it is often necessary to write a program which actually instantiates a template class or that calls a template function, since some types of coding errors only arise when instantiating a template, or using the instantiation.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    222

    Thumbs up

    Quote Originally Posted by grumpy View Post
    You are aware that, if you obtained a compiler, you could experiment with such questions, and learn more in the doing. Throwing random, undirected, and (mostly) trivial and pointless questions at a forum is an effective way to annoy other people by wasting their time, while minimising your chances of learning anything useful.

    You might also try explaining WHY you think line 2 is valid, but that lines 1 and 3 are not. The process of writing that description down will take more effort on your part, but you will learn more in the process, and annoy other people less when they see you applying genuine efforts to understand rather than asking increasingly pointless questions.

    Note that, with templates, it is often necessary to write a program which actually instantiates a template class or that calls a template function, since some types of coding errors only arise when instantiating a template, or using the instantiation.
    here line 1 already has forward declaration. So I could say friend declaration in line 2 is valid. Any suggestions?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Line 1 is not a forward declaration, and its presence doesn't affect whether line 2 is valid or not.

    Apparently you are selective. I made suggestions in my previous post, you ignored most of them, and asked for more.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Quote Originally Posted by grumpy View Post
    Line 1 is not a forward declaration, and its presence doesn't affect whether line 2 is valid or not.

    Apparently you are selective. I made suggestions in my previous post, you ignored most of them, and asked for more.
    since my assumption got wrong here, which one do you think is valid friend declaration? pls help me.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Re-read post #2.
    Elaborate on which ones you think is right/false, and why you think that.
    Then you will get answers that tells you if you are right/wrong and why.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    222
    Y template delcaration already exists, so line 2 is valid...any inputs?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, line 2 is correct.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 04-06-2011, 01:37 PM
  2. Replies: 3
    Last Post: 01-30-2011, 04:28 PM
  3. template parameter on a function that dont use the template
    By underthesun in forum C++ Programming
    Replies: 1
    Last Post: 06-22-2009, 05:38 PM
  4. Specialising a member function with a template template parameter
    By the4thamigo_uk in forum C++ Programming
    Replies: 10
    Last Post: 10-12-2007, 04:37 AM
  5. Replies: 4
    Last Post: 11-01-2006, 02:23 PM