473,395 Members | 2,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

template type problem in msvc. and more

I have some questions about template and c++ itself.

/////////////////////////////////////////////////////////////////////////
q1) see follow c++ code, and compile. it's works only IntelC++8.1 but
VC71.
Do you know why? Which compiler's activity is C++STANDARD? And I wanna
be
feed back some explain. :)

//-----------------------------------------------------------------------
//
// COMPILER_TEST!,
//
// a) VC71 fail to compile
// b) IC81 OK
//
//-----------------------------------------------------------------------
template < typename MY_TYPE_ >
class TestClass1
{
public:
typedef int TEST_TYPE;
};

template < typename MY_TYPE_ >
class TestClass2
{
public:
typedef MY_TYPE_ MY_TYPE;
typedef TestClass1<MY_TYPE> TEST_CLASS1;
typedef TestClass2<MY_TYPE> TEST_CLASS2;
typedef typename TEST_CLASS1::TEST_TYPE TEST_TYPE1;
//typename TestClass2<MY_TYPE_>:: // this make msvc happy, but I dont
know why!
TEST_TYPE1 TestMethod1();

//
// consequently,
// a) typename TestClass2<MY_TYPE_>::TEST_TYPE1
// and
// b) TEST_TYPE1
//
// is different type! why? its the same type!
//
// i think its result due to the "typename"
//

void TestMethod2( TEST_TYPE1 a );
};

/* this will emit error in MSVC */
template < typename MY_TYPE_ >
typename TestClass2<MY_TYPE_>::TEST_TYPE1 /* return type; return type
can not use types in TestClass2
it have to specify TestClass2 scope to compile*/
TestClass2<MY_TYPE_>::TestMethod1()
{
printf( "typename TestClass2<MY_TYPE_>::TEST_TYPE1
TestClass2<MY_TYPE_>::TestMethod1() - called.\r\n" );
return 0;
}

/* this is ok */
template < typename MY_TYPE_ >
void
TestClass2<MY_TYPE_>::TestMethod2(
//typename TestClass2<MY_TYPE_>:: //this var is on scope of
TestClass2, it can use types in TestClass2
TEST_TYPE1 a
)
{
return 0;
}
void test_2()
{
TestClass2<int> a;
a.TestMethod1();
}
//---------END_OF_CODE

/////////////////////////////////////////////////////////////////////////
q2)
code:
//-- start of code
class TestCalss3
{
public:
typedef int TEST_TYPE3;
TEST_TYPE3 TestMethod3( TEST_TYPE3 a );
};

inline
TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
{
return 1;
}
//-- end of code

Why c++ standard make difference between 'RETURN type' and 'ARGUMENT
type'?
I think 'new code1' or 'new code2' is looks better. Or I omit some
important point(s) about c++ design or compiler design?

//-- new code1
inline
TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
{
return 1;
}
//-- new code2
inline
TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TestCalss3::TEST_TYPE3
a )
{
return 1;
}
//--

/////////////////////////////////////////////////////////////////////////
I'll expect good reply. Thank you in advance.
ps excuse my BAD english :) I'm not native writer :0
Jul 23 '05 #1
1 1440

icedac wrote:
I have some questions about template and c++ itself.

///////////////////////////////////////////////////////////////////////// q1) see follow c++ code, and compile. it's works only IntelC++8.1 but
VC71.
Do you know why? Which compiler's activity is C++STANDARD? And I wanna be
feed back some explain. :)

//----------------------------------------------------------------------- //
// COMPILER_TEST!,
//
// a) VC71 fail to compile
// b) IC81 OK
//
//----------------------------------------------------------------------- template < typename MY_TYPE_ >
class TestClass1
{
public:
typedef int TEST_TYPE;
};

template < typename MY_TYPE_ >
class TestClass2
{
public:
typedef MY_TYPE_ MY_TYPE;
typedef TestClass1<MY_TYPE> TEST_CLASS1;
typedef TestClass2<MY_TYPE> TEST_CLASS2;
typedef typename TEST_CLASS1::TEST_TYPE TEST_TYPE1;
//typename TestClass2<MY_TYPE_>:: // this make msvc happy, but I dont know why!
TEST_TYPE1 TestMethod1(); I think it is a problem in VC. IMHO, those definitions are equivalent.

//
// consequently,
// a) typename TestClass2<MY_TYPE_>::TEST_TYPE1
// and
// b) TEST_TYPE1
//
// is different type! why? its the same type!
//
// i think its result due to the "typename"
// Don't know. Maybe you should post those questions also in a VC related
newsgroup.

void TestMethod2( TEST_TYPE1 a );
};

/* this will emit error in MSVC */
template < typename MY_TYPE_ >
typename TestClass2<MY_TYPE_>::TEST_TYPE1 /* return type; return type
can not use types in TestClass2
it have to specify TestClass2 scope to compile*/
TestClass2<MY_TYPE_>::TestMethod1()
{
printf( "typename TestClass2<MY_TYPE_>::TEST_TYPE1
TestClass2<MY_TYPE_>::TestMethod1() - called.\r\n" );
return 0;
}

/* this is ok */
template < typename MY_TYPE_ >
void
TestClass2<MY_TYPE_>::TestMethod2(
//typename TestClass2<MY_TYPE_>:: //this var is on scope of
TestClass2, it can use types in TestClass2
TEST_TYPE1 a
)
{
return 0;
}
Hm, it doesn't look ok. The method has void type as return type, but
you return 0. I know that VC is silent about that, but it should say
something, at least a warning.

void test_2()
{
TestClass2<int> a;
a.TestMethod1();
}
//---------END_OF_CODE

///////////////////////////////////////////////////////////////////////// q2)
code:
//-- start of code
class TestCalss3
{
public:
typedef int TEST_TYPE3;
TEST_TYPE3 TestMethod3( TEST_TYPE3 a );
};

inline
TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
{
return 1;
} Here I don't know how real 'a' type is deduced (there is no such type
in global namespace). Well, I'll search a little :). Comeau online also
compile fine this method implementation.
//-- end of code

Why c++ standard make difference between 'RETURN type' and 'ARGUMENT
type'?
I think 'new code1' or 'new code2' is looks better. Or I omit some
important point(s) about c++ design or compiler design?

//-- new code1
inline
TEST_TYPE3 TestCalss3::TestMethod3( TEST_TYPE3 a )
{
return 1;
} Not correct, as I said there isn't such a type in the global namespace
(equivalent with TestCalss3::TEST_TYPE3, eg. typedef
TestCalss3::TEST_TYPE3 TEST_TYPE3).
//-- new code2
inline
TestCalss3::TEST_TYPE3 TestCalss3::TestMethod3( TestCalss3::TEST_TYPE3 a )
{
return 1;
}
//--
This is ok :).

..
..
Best regards,
Bogdan Sintoma

Jul 23 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Nobody | last post by:
This is sort of my first attempt at writing a template container class, just wanted some feedback if everything looks kosher or if there can be any improvements. This is a template class for a...
5
by: Gianni Mariani | last post by:
The code below compiles on gcc 3.4.0 and Comeau's 4.3.3 but MSVC++ 7.1 dies complaining about somthing <unknown>. Is this valid ? More to the point, is there any way of doing this that is...
3
by: Dennis Pais | last post by:
Here's a piece of code that I found in C++ Primer by Stan Lippman ! ------------------------------------------------- template<typename T, int size> T min(T (&r_array) ) { Type min_value =...
3
by: Thomas Casanova | last post by:
Hi, I am compiling a MSVC++ project on linux gcc 3.3.4. gcc is whimsical about templates. It does understand the declaration of the template type PointerList. It scream the error :...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
2
by: jeffp | last post by:
The following snipped of code won't compiler under either MSVC 7 or GCC (not sure which version). I can't think of any reason it should not work, anybody have any ideas: template <typename T>...
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
2
by: Glenn G. Chappell | last post by:
I am trying to write two constructors for the same class. One takes an iterator and so is a template. The other takes a particular type by reference to const. class Foo { public:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.