Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to do full explicit specification?

Reply
Thread Tools

How to do full explicit specification?

 
 
Nephi Immortal
Guest
Posts: n/a
 
      01-16-2013
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 >:()
{
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;
}
 
Reply With Quote
 
 
 
 
Zhihao Yuan
Guest
Posts: n/a
 
      01-16-2013
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/
 
Reply With Quote
 
 
 
 
Nephi Immortal
Guest
Posts: n/a
 
      01-16-2013
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.

 
Reply With Quote
 
Zhihao Yuan
Guest
Posts: n/a
 
      01-16-2013
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/
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      01-16-2013
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/


 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      01-16-2013
On Wednesday, January 16, 2013 11:17:54 AM UTC-6, Paavo Helde wrote:
> Nephi Immortal <(E-Mail Removed)> wrote in
>
> news:(E-Mail Removed):
>
>
>
> >

>
> > 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.
 
Reply With Quote
 
Nephi Immortal
Guest
Posts: n/a
 
      01-16-2013
On Wednesday, January 16, 2013 1:35:41 PM UTC-6, Paavo Helde wrote:
> Nephi Immortal <(E-Mail Removed)> wrote in
>
> news:(E-Mail Removed):
>
>
>
> > On Wednesday, January 16, 2013 11:17:54 AM UTC-6, Paavo Helde wrote:

>
> >> Nephi Immortal <(E-Mail Removed)> wrote in

>
> >>

>
> >> news:(E-Mail Removed):

>
> >>

>
> >>

>
> >>

>
> >> >

>
> >>

>
> >> > 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?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Explicit instantiation of STL vector demands explicit instantiation of all the templates it using internally. krunalbauskar@gmail.com C++ 1 12-25-2006 03:51 PM
Partial but not full/explicit specialization at non-namespace scope. Ferdi Smit C++ 8 10-19-2005 10:27 PM
What's the difference betwwen explicit instantiaion and explicit specialization? Andy C++ 5 01-30-2005 11:46 PM
Is explicit template qualification required for explicit delete? J.T. Conklin C++ 1 08-11-2004 02:06 AM



Advertisments