 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Hanyou Chu Guest
|
Posted: Tue Aug 19, 2003 12:29 am Post subject: MSVC 7. won't compile these |
|
|
The following code compiles fine with VC 6.0 and
Intel 7.0 compilers, but wouldn't compile with VC 7.0.
struct vec2d{
double x, y;
typedef double TNorm;
// .........
};
struct vec3d{
double x,y,z;
typedef vec3d TNormal;
// .........
};
template <class Tvec>
struct triangle{
Tvec v1,v2,v3;
Tvec::TNorm normal;
};
template triangle<vec2d>;
template triangle<vec3d>;
To make sure that things work fine, I found the following
code from the internet and tested with VC 7.0 and found out
that it won't compile either:
template <class numT>
struct float_traits { };
struct float_traits<float> {
typedef float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
};
struct float_traits<double> {
typedef double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
};
template <class numT>
class matrix {
public:
typedef numT num_type;
typedef float_traits<num_type> traits_type;
inline num_type epsilon() { return traits_type::epsilon(); }
};
template class matrix<double>;
Any help would be appreciated.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Petre Iantu Guest
|
Posted: Tue Aug 19, 2003 9:31 am Post subject: Re: MSVC 7. won't compile these |
|
|
this code compiles with vc71
struct vec2d{
double x, y;
typedef double TNorm;
// .........
};
struct vec3d{
double x,y,z;
typedef vec3d TNormal;
// .........
};
template <class Tvec>
struct triangle{
Tvec v1,v2,v3;
typename vec2d::TNorm normal;
};
template triangle<vec2d>;
template triangle<vec3d>;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Spangenberg Guest
|
Posted: Tue Aug 19, 2003 10:42 am Post subject: Re: MSVC 7. won't compile these |
|
|
Hello, Hanyou Chu!
Hanyou Chu schrieb:
| Quote: | The following code compiles fine with VC 6.0 and
Intel 7.0 compilers, but wouldn't compile with VC 7.0.
struct vec2d{
double x, y;
typedef double TNorm;
// .........
};
struct vec3d{
double x,y,z;
typedef vec3d TNormal;
// .........
};
template <class Tvec
struct triangle{
Tvec v1,v2,v3;
Tvec::TNorm normal;
|
The final declaration must be written as:
typename Tvec::TNorm normal;
| Quote: | };
template triangle
template triangle<vec3d>;
|
Here are two different errors:
1) The explicit instantiation of a complete class template must be
written as
template class triangle<vec3d>;
It is also OK, to use the struct keyword in this case:
template struct triangle<vec3d>;
2) The 2nd of these instantiations should not compile, because it
expects a TNorm
typename from its template argument, but vec3d does not provide such a
typename
(It provides the TNormal typename. Is this by design?)
| Quote: | To make sure that things work fine, I found the following
code from the internet and tested with VC 7.0 and found out
that it won't compile either:
|
In general, you should explain the compilation errors, because there are
often
several possibilities (e.g. missing to #include necessary headers...),
which
are not deducable from our limited point of view.
| Quote: | template <class numT
struct float_traits { };
struct float_traits
typedef float float_type;
enum { max_exponent = FLT_MAX_EXP };
static inline float_type epsilon() { return FLT_EPSILON; }
};
|
This is an invalid full class template specialization. The declaration
should start with
template <>
struct float_traits<float> {
....
Some older compilers accepted the missing "template <>", but this
is a non-portable syntax.
| Quote: | struct float_traits<double> {
typedef double float_type;
enum { max_exponent = DBL_MAX_EXP };
static inline float_type epsilon() { return DBL_EPSILON; }
};
|
Same missing "template <>" missing here in front of struct
float_traits<double>.
| Quote: |
template <class numT
class matrix {
public:
typedef numT num_type;
typedef float_traits
inline num_type epsilon() { return traits_type::epsilon(); }
};
template class matrix<double>;
|
The syntax seems OK here, but there are some design flaws:
1) Why needs the matrix class template to provide the epsilon function?
Normally the same information is directly available via
traits_type::epsilon(), because traits_type is available.
2) If so, this epsilon() member function is unnecessarily non-const and
even non-static. If you really want to provide it in the matrix
interface,
use a static function, because its call needs no access to non-static
class members:
template <class numT>
class matrix {
public:
typedef numT num_type;
typedef float_traits<num_type> traits_type;
static num_type epsilon() { return traits_type::epsilon(); }
};
3) You should be aware of the existence of the std::numeric_limits class
template, which provides essentially the information, which your
float_traits class template provides (and much more), thus you could
have
provided the typedef:
typedef std::numeric_limits<num_type> traits_type;
in the matrix class template.
Hope that helps,
Daniel Spangenberg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Tue Aug 19, 2003 12:31 pm Post subject: Re: MSVC 7. won't compile these |
|
|
In article <ceee3b79.0308181110.104e5309 (AT) posting (DOT) google.com>,
Hanyou Chu wrote:
| Quote: | The following code compiles fine with VC 6.0 and
Intel 7.0 compilers, but wouldn't compile with VC 7.0.
|
You should post error messages and identify which line(s) they refer
to. You should also post your actual code rather than re-typing it.
However, I can guess what the problem is already.
| Quote: | struct vec2d{
double x, y;
typedef double TNorm;
// .........
};
struct vec3d{
double x,y,z;
typedef vec3d TNormal;
|
Did you mean "typedef vec2d TNorm;"?
| Quote: | // .........
};
template <class Tvec
struct triangle{
Tvec v1,v2,v3;
Tvec::TNorm normal;
|
The compiler cannot know in advance what kind of thing the "TNorm"
member of its type parameter will be. You must include the keyword
"typename" in front of this line to tell it that Tvec::TNorm will be
a type, so that it can be parsed correctly.
Older compilers did not completely parse template definitions, so
they did not have this problem. The standard specifies a behaviour
called "two-phase name lookup" which requires this.
| Quote: | };
template triangle
template triangle<vec3d>;
To make sure that things work fine, I found the following
code from the internet and tested with VC 7.0 and found out
that it won't compile either:
template <class numT
struct float_traits { };
struct float_traits
snip |
This is wrong; template specialisations must begin with
"template<>".
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Hanyou Chu Guest
|
Posted: Wed Aug 20, 2003 1:49 am Post subject: Re: MSVC 7. won't compile these |
|
|
| Quote: | The final declaration must be written as:
typename Tvec::TNorm normal;
Yes, this works!
};
template triangle<vec2d>;
template triangle<vec3d>;
Here are two different errors:
1) The explicit instantiation of a complete class template must be
written as
template class triangle<vec3d>;
It is also OK, to use the struct keyword in this case:
template struct triangle<vec3d>;
2) The 2nd of these instantiations should not compile, because it
expects a TNorm
typename from its template argument, but vec3d does not provide such a
typename
(It provides the TNormal typename. Is this by design?)
|
Yes this is by design, the errors had nothing to do with instantiation.
| Quote: |
template
struct float_traits<float> {
...
|
Yes this does the trick.
| Quote: | Some older compilers accepted the missing "template <>", but this
is a non-portable syntax.
I need to catch up with the C++ standards. |
For those who read my message, sorry for not providing the error messages.
I typed things in a hurry and forgot to do so.
Thanks a lot!
Hanyou Chu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|