Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

class specialization; is it me or msvc?

2 views
Skip to first unread message

chuck starchaser

unread,
Sep 15, 2002, 2:38:48 AM9/15/02
to
template <class T>
class color
{
T data_[4];
................
public:
operator GLfloat const * () const
{
throw( "unimplemented conversion" );
}
operator GLfloat * ()
{
throw( "unimplemented conversion" );
}
................
};


template <>
color<float>::operator GLfloat const * () const
{
return data_;
}
color<float>::operator GLfloat * ()
{
return data_;
}


error C2556: '__thiscall color<float>::operator`float *'(void) const'
: overloaded function differs only by return type from '__thiscall
color<float>::operator`const float *'(void) const'
e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
declaration of 'operator`const float *''


Is it me or MSVC (6.0 with sp5) ?

Magnus Lidbom

unread,
Sep 15, 2002, 6:36:38 AM9/15/02
to

"chuck starchaser" <chuck_st...@email.com> wrote in message
news:qba8oukp6821ekp0r...@4ax.com...

<snipped code>

> error C2556: '__thiscall color<float>::operator`float *'(void) const'
> : overloaded function differs only by return type from '__thiscall
> color<float>::operator`const float *'(void) const'
> e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
> declaration of 'operator`const float *''
>
>
> Is it me or MSVC (6.0 with sp5) ?

It's you :)
At least VC6 VC.Net and g++ 3.2 agree. I won't swear about what the standard
says :)

Here's the problem:
1. You need to create the class specialization explicitly.
2. You need to return &data_ not data_.

The usage looks pretty dubiuos to me but the code below will
compile, run, and do what you expect. I wouldn't recommend using it though :)
Looks to unsafe to me. Returning pointers to private arrays is not really a
good idea imho.

Regards /Magnus

#include <iostream>

using namespace std;

typedef float GLfloat[4];

template <class T>
class color
{
T data_[4];

public:
operator GLfloat const * () const
{
throw( "unimplemented conversion" );
}
operator GLfloat * ()
{
throw( "unimplemented conversion" );
}

};

template <>
class color<float>
{
float data_[4];
public:
color<float>()
{
data_[0] = 255;
data_[1] = 255;
data_[2] = 255;
data_[3] = 255;
}

operator GLfloat const * () const;
operator GLfloat * ();
};

color<float>::operator GLfloat const * () const
{

return &data_;
}

color<float>::operator GLfloat * ()
{

return &data_;
}


int main()
{
color<float> myfloatColor;
GLfloat* myGLfloat;
myGLfloat = myfloatColor;

for(int i = 0; i < 4; ++i)
cout << (*myGLfloat)[i] << endl;


return 0;
}


chuck starchaser

unread,
Sep 15, 2002, 6:48:08 PM9/15/02
to
On Sun, 15 Sep 2002 10:36:38 GMT, "Magnus Lidbom"
<magnus...@hotmail.com> wrote:


>template <>
>class color<float>
>{
> float data_[4];
>public:
> color<float>()
> {
> data_[0] = 255;
> data_[1] = 255;
> data_[2] = 255;
> data_[3] = 255;
> }
>
> operator GLfloat const * () const;
> operator GLfloat * ();
>};
>
>color<float>::operator GLfloat const * () const
>{
> return &data_;
>}
>
>color<float>::operator GLfloat * ()
>{
> return &data_;
>}
>
>
>int main()
>{
> color<float> myfloatColor;
> GLfloat* myGLfloat;
> myGLfloat = myfloatColor;
>
> for(int i = 0; i < 4; ++i)
> cout << (*myGLfloat)[i] << endl;
>
>
> return 0;
>}
>

Oh yes!! Thanks!


tom_usenet

unread,
Sep 16, 2002, 5:42:41 AM9/16/02
to
On Sun, 15 Sep 2002 02:38:48 -0400, chuck starchaser
<chuck_st...@email.com> wrote:


typedef float GLfloat;

>template <class T>
>class color
>{
> T data_[4];
> ................
>public:
> operator GLfloat const * () const

You'd be better off leaving out the implementation entirely, so the
mistake is found at link time.

operator GLfloat const*() const; //unimplemented.

> {
> throw( "unimplemented conversion" );
> }
> operator GLfloat * ()
> {
> throw( "unimplemented conversion" );
> }
> ................
>};
>
>
>template <>
>color<float>::operator GLfloat const * () const
>{
> return data_;
>}

template<>

>color<float>::operator GLfloat * ()
>{
> return data_;
>}
>
>
>error C2556: '__thiscall color<float>::operator`float *'(void) const'
>: overloaded function differs only by return type from '__thiscall
>color<float>::operator`const float *'(void) const'
> e:\ogl\tutorials\oo-glbase\glcad\color.h(124) : see
>declaration of 'operator`const float *''
>
>
>Is it me or MSVC (6.0 with sp5) ?

MSVC 6.0. It is legal to specialise member functions independently of
the surrounding class.

Compiles fine on gcc 3.2 and como 4.3, with the changes above.

Tom

0 new messages