How to do full explicit specification?

N

Nephi Immortal

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;
}
 
Z

Zhihao Yuan

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.
 
N

Nephi Immortal

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.

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.
 
Z

Zhihao Yuan

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.
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`.
 
N

Nephi Immortal

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

Nephi Immortal

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.
 
N

Nephi Immortal

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.

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top