How to do full explicit specification?

Discussion in 'C++' started by Nephi Immortal, Jan 16, 2013.

  1. How can I fix partial explicit specification on Go() function before I can declare full explicit specification on struct Outer?

    // Outer.h
    enum eTypes
    {
    eA,
    eB
    };

    template< eTypes eType, typename Type >
    struct Outer
    {
    template< eTypes eType, typename Enable >
    struct Inner;

    template< typename Enable >
    struct Inner< eA, Enable >
    {
    Inner();
    ~Inner();

    Type A();
    Type B();
    };

    template< typename Enable >
    struct Inner< eB, Enable >
    {
    Inner();
    ~Inner();

    Type A();
    Type B();
    Type C();
    Type D();
    };

    Outer();
    ~Outer();

    Inner< eType, void >
    Go();
    };

    // Outer.cpp
    template<>
    template<>
    Outer< eA, long >::Inner< eA, void >::Inner()
    {
    }

    template<>
    template<>
    Outer< eA, long >::Inner< eA, void >::~Inner()
    {
    }

    template<>
    template<>
    long
    Outer< eA, long >::Inner< eA, void >::A()
    {
    return 1;
    }

    template<>
    template<>
    long
    Outer< eA, long >::Inner< eA, void >::B()
    {
    return 2;
    }





    template<>
    template<>
    Outer< eB, long long >::Inner< eB, void >::Inner()
    {
    }

    template<>
    template<>
    Outer< eB, long long >::Inner< eB, void >::~Inner()
    {
    }

    template<>
    template<>
    long long
    Outer< eB, long long >::Inner< eB, void >::A()
    {
    return 1;
    }

    template<>
    template<>
    long long
    Outer< eB, long long >::Inner< eB, void >::B()
    {
    return 2;
    }

    template<>
    template<>
    long long
    Outer< eB, long long >::Inner< eB, void >::C()
    {
    return 3;
    }

    template<>
    template<>
    long long
    Outer< eB, long long >::Inner< eB, void >::D()
    {
    return 4;
    }

    template< eTypes eType, typename Type >
    Outer< eType, Type >::Outer()
    {
    }

    template< eTypes eType, typename Type >
    Outer< eType, Type >::~Outer()
    {
    }

    template< eTypes eType, typename Type >
    Outer< eType, Type >::Inner< eType, void >
    Outer< eType, Type >::Go()
    {
    return Inner< eType, void >();
    }

    template
    struct Outer< eA, long >;

    template
    struct Outer< eB, long long >;

    // main.cpp
    #include “Outer.h”

    int main()
    {
    long data = 0;

    Outer< eA, long > a;
    Outer< eB, long long > b;

    data = a.Go().A();
    data = b.Go().C();
    return 0;
    }
     
    Nephi Immortal, Jan 16, 2013
    #1
    1. Advertising

  2. Nephi Immortal

    Zhihao Yuan Guest

    On Tuesday, January 15, 2013 9:02:31 PM UTC-6, Nephi Immortal wrote:
    > How can I fix partial explicit specification on Go() function before I can declare full explicit specification on struct Outer?


    Can you provide some mock-up code you want to write? Because I have
    no idea about what you want to do, especially after I compiled and ran
    your code pasted without a problem...

    Note: If you want to partial specialize the function template
    `Go()`, you probably have to partial specialize a class template with
    a static member function inside.

    > template< eTypes eType, typename Type >
    > struct Outer
    > {
    > template< eTypes eType, typename Enable >


    BTW, the nested `eType` here shadows the outer one; rename it please.

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
     
    Zhihao Yuan, Jan 16, 2013
    #2
    1. Advertising

  3. On Tuesday, January 15, 2013 9:59:06 PM UTC-6, Zhihao Yuan wrote:
    > On Tuesday, January 15, 2013 9:02:31 PM UTC-6, Nephi Immortal wrote:
    >
    > > How can I fix partial explicit specification on Go() function before I can declare full explicit specification on struct Outer?

    >
    >
    >
    > Can you provide some mock-up code you want to write? Because I have
    >
    > no idea about what you want to do, especially after I compiled and ran
    >
    > your code pasted without a problem...
    >
    >
    >
    > Note: If you want to partial specialize the function template
    >
    > `Go()`, you probably have to partial specialize a class template with
    >
    > a static member function inside.
    >
    >
    >
    > > template< eTypes eType, typename Type >

    >
    > > struct Outer

    >
    > > {

    >
    > > template< eTypes eType, typename Enable >


    All member functions work fine except Go(). If I create two separate Go() functions by full specialization, then I must create two copies each function such as constructor and destructor and remove two full specification classes.

    I do not want to do that. Two version of inner classes can have full specification and one version of outer class can have partial specification before I can explicitly declare full specification on class in the source code.

    Partial Specification should be:

    // Unable to compile
    template< eTypes eType, typename Type >
    Outer< eType, Type >::Inner< eType, void >
    Outer< eType, Type >::Go()
    {
    return Inner< eType, void >();
    }

    Instead of

    Full Specification

    // Able to compile successfully
    Outer< eA, long >::Inner< eA, void >
    Outer< eA, long >::Go()
    {
    return Inner< eA, void >();
    }
    Outer< eB, long long >::Inner< eB, void >
    Outer< eB, long long >::Go()
    {
    return Inner< eB, void >();
    }

    And then I can use below.

    template
    struct Outer< eA, long >;

    template
    struct Outer< eB, long long >;

    Nested class is very generic. First version of inner class can have two functions: A(), B() and second version of inner class can have four functions: A(), B(), C(), and D(). You can choose which version you want to limit the number of functions.

    > BTW, the nested `eType` here shadows the outer one; rename it please.


    I am not sure I understand your question.

    > --
    >
    > Zhihao Yuan, ID lichray
    >
    > The best way to predict the future is to invent it.


    What will you do in the future? Next life? You will live on earth forever while you will be immortal being. The earth represents to be heaven. There are numberless (trillions and trillions of) earths outside our solar system.
     
    Nephi Immortal, Jan 16, 2013
    #3
  4. Nephi Immortal

    Zhihao Yuan Guest

    On Tuesday, January 15, 2013 11:09:11 PM UTC-6, Nephi Immortal wrote:
    > Partial Specification should be:
    >
    > // Unable to compile
    > template< eTypes eType, typename Type >
    > Outer< eType, Type >::Inner< eType, void >
    > Outer< eType, Type >::Go()
    > {
    > return Inner< eType, void >();
    > }


    I can compile your code with gcc42,46,48, clang-3.2, no problem.
    Running, OK. Check your compiler and the error message.

    > > BTW, the nested `eType` here shadows the outer one; rename it please.

    >
    > I am not sure I understand your question.


    Inside

    template< eTypes eType, typename Type >
    struct Outer

    you defined

    template< eTypes eType, typename Enable >
    struct Inner;

    , so the template parameter `eType` appeared twice. Rename the
    second one into something else, like `eTypeInner`.

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
     
    Zhihao Yuan, Jan 16, 2013
    #4
  5. On Tuesday, January 15, 2013 11:38:36 PM UTC-6, Zhihao Yuan wrote:
    > On Tuesday, January 15, 2013 11:09:11 PM UTC-6, Nephi Immortal wrote:
    >
    > > Partial Specification should be:

    >
    > >

    >
    > > // Unable to compile

    >
    > > template< eTypes eType, typename Type >

    >
    > > Outer< eType, Type >::Inner< eType, void >

    >
    > > Outer< eType, Type >::Go()

    >
    > > {

    >
    > > return Inner< eType, void >();

    >
    > > }

    >
    >
    >
    > I can compile your code with gcc42,46,48, clang-3.2, no problem.
    >
    > Running, OK. Check your compiler and the error message.


    Then why Visual C++ 2010 / 2012 compiled with error?

    > > > BTW, the nested `eType` here shadows the outer one; rename it please.

    >
    > >

    >
    > > I am not sure I understand your question.

    >
    >
    >
    > Inside
    >
    >
    >
    > template< eTypes eType, typename Type >
    >
    > struct Outer
    >
    >
    >
    > you defined
    >
    >
    >
    > template< eTypes eType, typename Enable >
    >
    > struct Inner;
    >
    >
    >
    > , so the template parameter `eType` appeared twice. Rename the
    >
    > second one into something else, like `eTypeInner`.
    >
    >
    >
    > --
    >
    > Zhihao Yuan, ID lichray
    >
    > The best way to predict the future is to invent it.
    >
    > ___________________________________________________
    >
    > 4BSD -- http://4bsd.biz/
     
    Nephi Immortal, Jan 16, 2013
    #5
  6. On Wednesday, January 16, 2013 11:17:54 AM UTC-6, Paavo Helde wrote:
    > Nephi Immortal <> wrote in
    >
    > news::
    >
    >
    >
    > >

    >
    > > Then why Visual C++ 2010 / 2012 compiled with error?

    >
    >
    >
    > Not informative at all. What is the error message? There might be linker
    >
    > errors and missing 'typename' issues.


    error C2143: syntax error : missing ';' before 'Omega::Outer<eType,Type>::Go'

    I don't see anything wrong with Go(). Please test my code on VS 2010 / 2012.
     
    Nephi Immortal, Jan 16, 2013
    #6
  7. On Wednesday, January 16, 2013 1:35:41 PM UTC-6, Paavo Helde wrote:
    > Nephi Immortal <> wrote in
    >
    > news::
    >
    >
    >
    > > On Wednesday, January 16, 2013 11:17:54 AM UTC-6, Paavo Helde wrote:

    >
    > >> Nephi Immortal <> wrote in

    >
    > >>

    >
    > >> news::

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> >

    >
    > >>

    >
    > >> > Then why Visual C++ 2010 / 2012 compiled with error?

    >
    > >>

    >
    > >>

    >
    > >>

    >
    > >> Not informative at all. What is the error message? There might be

    >
    > >> linker

    >
    > >>

    >
    > >> errors and missing 'typename' issues.

    >
    > >

    >
    > > error C2143: syntax error : missing ';' before

    >
    > > 'Omega::Outer<eType,Type>::Go'

    >
    >
    >
    > There is no such word "Omega" in the code you posted. Post code and error
    >
    > messages which match each other!


    'Omega' does not cause C++ Compiler to generate in errors.
    I do not need to include 'Omega' in the code, but I forgot to
    remove it. It is the namespace before Outer class.


    > > I don't see anything wrong with Go(). Please test my code on VS 2010

    >
    > > / 2012.

    >
    >
    >
    > VS2010 gave a missing typename error for Go() in your *posted* code,
    >
    > after I added 'typename' it compiled fine (all the code in a single
    >
    > file):
    >
    >
    >
    > template< eTypes eType, typename Type >
    >
    > typename Outer< eType, Type >::Inner< eType, void >
    >
    > Outer< eType, Type >::Go()
    >
    > {
    >
    > return Inner< eType, void >();
    >
    > }


    Ok, I follow the code above by adding typename. Will GCC 4.xx
    generate errors if you remove typename before Outer class?
     
    Nephi Immortal, Jan 16, 2013
    #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. J.T. Conklin
    Replies:
    1
    Views:
    443
    David Hilsee
    Aug 11, 2004
  2. Andy
    Replies:
    5
    Views:
    509
    Shezan Baig
    Jan 30, 2005
  3. Replies:
    1
    Views:
    580
    Salt_Peter
    Dec 25, 2006
  4. Kenneth McDonald

    Full RDoc specification?

    Kenneth McDonald, Jul 7, 2007, in forum: Ruby
    Replies:
    1
    Views:
    103
    Anupam Sengupta
    Jul 7, 2007
  5. Nephi Immortal

    invalid explicit specification?

    Nephi Immortal, Feb 7, 2013, in forum: C++
    Replies:
    5
    Views:
    254
    Rosario1903
    Feb 10, 2013
Loading...

Share This Page