Like Tree7Likes
  • 7 Post By grumpy

template in c++

This is a discussion on template in c++ within the C++ Programming forums, part of the General Programming Boards category; which is a valid friend declaration for class template given below. I guess line 2 is valid...any suggestions? Code: template<class ...

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

    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,295
    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%.

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

    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,295
    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%.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    162
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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
    162
    Y template delcaration already exists, so line 2 is valid...any inputs?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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, 03: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, 01:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21