Like Tree3Likes
  • 1 Post By grumpy
  • 1 Post By kmdv
  • 1 Post By Elkvis

Template specialization not working in Code::Blocks...

This is a discussion on Template specialization not working in Code::Blocks... within the C++ Programming forums, part of the General Programming Boards category; hey all, i tried to do what is shown in the lesson Template specialization in cprogramming.com... but i get the ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    36

    Template specialization not working in Code::Blocks...

    hey all, i tried to do what is shown in the lesson Template specialization in cprogramming.com...

    but i get the following error:
    Name:  untitled.jpg
Views: 585
Size:  71.6 KB

    what do i do?
    moreover i wanted to ask what is the meaning of the tutorial.
    is the thing taught in the lesson at all useful?
    what will be the consequences if i skip this lesson?
    Last edited by tennisstar; 12-17-2012 at 04:24 AM.

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You need the generic class template definition before trying to specialize it.
    Read a tutorial on templates if you already haven't.

    You can skip this now and learn about it when you need to use it for the first time.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    To be able to specialise a template, the compiler needs to see both the template itself, for example ....
    Code:
    template<class T> class alpha
    {
         // whatever
    };
    as well as the specialisation of that template.
    Code:
    template<> class alpha<int>
    {
         // whatever
    };
    Your code has the specialisation, but not the template that is being specialised. The compiler needs to see both, and will complain bitterly if it doesn't.
    Sajas K K likes this.
    Right 98% of the time, and don't care about the other 3%.

  4. #4
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    i'd like to make some things clear:
    1) I know about templates and the generic class template definition is right on the line 7
    2) Mr grumpy. i have done exactly what you have showed in your 2nd example

    it doesn't work...

    3)Mr manasij7479, i have a knowledge of the templates i learnt it from cprogramming.com...(Check this page: http://www.cprogramming.com/tutorial.html , i am thorough with everything in the "C++ Tutorial, C++ Made Easy: Learning to Program in C++" except the tutorial: Template specialization and partial specialization [thats is why i am asking qs. about the lesson])

    PS. if you think that since i have made the template and class definitions on separate lines it wont work.. i have tried it out for normal templates and it does work:

    Code:
    template <typename T> class x{
    
    
     //THIS WORKS!
    
    
    };
    this also works:
    Code:
    template <typename T>
     class x{
    
    
     //THIS WORKS!
    
    
    };
    Last edited by tennisstar; 12-17-2012 at 06:28 AM.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Then show us the smallest possible compilable example that demonstrates the problem.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    682
    Quote Originally Posted by manasij7479 View Post
    You need the generic class template definition before trying to specialize it.
    No, just the declaration.

    Code:
    // ** This declaration has to be added. **
    template <class T>
    class Alpha;
    
    ...
    
    template <>
    class Alpha<int>
    {
    };
    
    int main()
    {
        Alpha<int> a1;
        // Alpha<long> a2;  // This won't work, because Alpha<long> is an incomplete type.
        return 0;
    }
    Simply add the above declaration.
    manasij7479 likes this.
    I never put signature, but I decided to make an exception.

  7. #7
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Quote Originally Posted by kmdv View Post
    No, just the declaration.
    Didn't know that, thanks.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by tennisstar View Post
    i'd like to make some things clear:
    1) I know about templates and the generic class template definition is right on the line 7
    That's not the generic template, that's the specialisation, which as others have said needs to come after the generic template definition, or at least its declaration.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,306
    Quote Originally Posted by tennisstar View Post
    2) Mr grumpy. i have done exactly what you have showed in your 2nd example

    it doesn't work...
    Then you didn't do exactly what I described.

    What I described is that the compiler needs to see the template definition BEFORE it sees the specialisation. (or as kmdv pointed out, the declaration before the specialisation).
    Right 98% of the time, and don't care about the other 3%.

  10. #10
    Registered User
    Join Date
    Jul 2012
    Posts
    36
    Thankyou kmdv,
    You answered what exactly i was asking...
    i made a sample program using the things i understood;
    Code:
    include <iostream>
    using namespace std;
    
    
    template <typename T>
    class Alpha;
    
    
    template <>
    class Alpha <int>{
    
    
        public:
            Alpha(){
    
    
                cout << "INTEGER";
    
    
            }
    
    
    };
    
    
    template <>
    class Alpha <double>{
    
    
        public:
            Alpha(){
    
    
                cout << "DOUBLE";
    
    
            }
    
    
    };
    int main(){
    
    
        Alpha <int> a;
        Alpha <double> b;
    
    
    }
    this program should display: INTEGERDOUBLE
    one more thing. what i deduced from the lesson is that by using the "template <>" thing, you can cause the template to work in different ways for different datatypes... while normal templates work for all data types but they work the same everytime...

    i just wanna know if that assumption is correct...

    i also found an alternate method to make your template work in different ways depending on the datatype
    here it is:
    Code:
    #include <iostream>
    using namespace std;
    
    
    template <typename T>
    class Alpha{
    
    
        public:
            Alpha(){
    
    
                if(sizeof(T) == sizeof(int)){
    
    
                    cout << "INTEGER\n";
    
    
                }else if(sizeof(T) == sizeof(double)){
    
    
                    cout << "DOUBLE\n";
    
    
                }else if(sizeof(T) == sizeof(char)){
    
    
                    cout << "CHARACTER\n";
    
    
                }else if(sizeof(T) == sizeof(float)){
    
    
                    cout << "FLOAT\n";
    
    
                }else{
    
    
                    cout << "DUNNO!";
    
    
                }
    
    
            }
    
    
    };
    
    
    int main(){
    
    
        Alpha <int> a1;
        Alpha <double> a2;
        Alpha <char> a3;
        Alpha <float> a4;
        Alpha <short> a5;
    
    
    }
    i just wanna know which way is a better way..

  11. #11
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Something like the following would be more reliable than sizeof.
    Code:
    #include<typeinfo>
    #include<iostream>
    template<typename T>
    struct Alpha
    {
        Alpha()
        {
            std::cout<<typeid(T).name();
        }
    };
    int main()
    {
        Alpha<double> foo;
    }
    Quote Originally Posted by tennisstar View Post
    i just wanna know which way is a better way..
    Depends on the exact usage scenario.
    In general the specialized verision will do everything at compile time, thus having slightly better performance.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,421
    Quote Originally Posted by manasij7479 View Post
    Something like the following would be more reliable than sizeof.
    but use of RTTI is usually a sign of a bad design. I understand the need to see the type at runtime for learning purposes, but in production code, it's generally a bad idea.
    manasij7479 likes this.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,614
    Quote Originally Posted by tennisstar View Post
    i just wanna know which way is a better way..
    The former, because it infers no runtime overhead.
    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.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    Quote Originally Posted by tennisstar View Post
    what i deduced from the lesson is that by using the "template <>" thing, you can cause the template to work in different ways for different datatypes... while normal templates work for all data types but they work the same everytime...
    That's what template specialisation is. There is also partial specialisation, but that's not as common so lets no go there yet.


    i also found an alternate method to make your template work in different ways depending on the datatype
    here it is:
    <snip>

    i just wanna know which way is a better way..
    Yuck! Can you imagine if the standard C++ library was like that?
    First of all, it would not work at all because there are plenty of data types that have the same size, and you can create an infinite number of classes that again have the same size.
    Then there's the fact that it is deducing all of what to do at run-time, and the resulting executable would contain the code for every possible data type even if that code is never used.
    If you wanted to add something new then you'd have to modify the code! In fact you'd pretty much need to copy and paste the code first, removing the whole benefit of templates.
    I'd stop just short of saying that would be the worst possible way to do it, because someone determined could certainly come up with something even worse if given enough time.
    Last edited by iMalc; 12-18-2012 at 11:33 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code::Blocks not working?
    By C++Froob in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2010, 11:47 AM
  2. Help with template specialization
    By fairguynova in forum C++ Programming
    Replies: 10
    Last Post: 03-07-2009, 01:59 AM
  3. template specialization
    By CodeMonkey in forum C++ Programming
    Replies: 3
    Last Post: 12-29-2008, 01:02 AM
  4. Template specialization
    By CornedBee in forum C++ Programming
    Replies: 2
    Last Post: 11-25-2003, 01:02 AM
  5. Template specialization
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-19-2002, 07:08 AM

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