473,785 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference between generics and templates

Hi,

I suppose this question was already often asked and in fact I am not
interested in a complete overview of the differences, cause I already
found them on several pages.

In fact, what I want to know is the following. What is meant by:
"Here's a subtle point, but it's a direct result of how types are
identified in .NET. Let's say that C++ code in assembly A has used a
List<int> template specialization and that C++ code in assembly B
expects to use that exact same specialized type. Code in assembly A
creates an instance of the List<int> type and passes a reference to that
instance to code in assembly B that wishes to use its List<int> type.
Boom! At the very least, you will get a runtime type mismatch exception.
Depending on how you pass the reference, you might even get the error at
compile time. The error is because the List<int> type in assembly A is
not the same type as List<int> in assembly B."
(Taken from:
http://www.developer.com/net/cplus/a...919_3367531_1).

What does the author means by assembly? Is this a class?

So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)

Is this the problem described above, or what does the author means?

Thanks for your help!
Anton
Jan 16 '06 #1
9 2344

Anton Berg wrote:
Hi,

I suppose this question was already often asked and in fact I am not
interested in a complete overview of the differences, cause I already
found them on several pages.

In fact, what I want to know is the following. What is meant by:
"Here's a subtle point, but it's a direct result of how types are
identified in .NET. Let's say that C++ code in assembly A has used a
List<int> template specialization and that C++ code in assembly B
expects to use that exact same specialized type. Code in assembly A
creates an instance of the List<int> type and passes a reference to that
instance to code in assembly B that wishes to use its List<int> type.
Boom! At the very least, you will get a runtime type mismatch exception.
Depending on how you pass the reference, you might even get the error at
compile time. The error is because the List<int> type in assembly A is
not the same type as List<int> in assembly B."
(Taken from:
http://www.developer.com/net/cplus/a...919_3367531_1).

What does the author means by assembly? Is this a class?

So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)

Is this the problem described above, or what does the author means?

Thanks for your help!
Anton

It was never answered b/c it is OT - off-topic in this group! Try
comp.compilers. javacc

Jan 16 '06 #2
Anton Berg wrote:
I suppose this question was already often asked and in fact I am not
interested in a complete overview of the differences, cause I already
found them on several pages.

In fact, what I want to know is the following. What is meant by:
"Here's a subtle point, but it's a direct result of how types are
identified in .NET.
..NET is its own universe and it has some areas that do not intersect with
our, Standard C++, universe. You should consider asking in a .NET forum
about .NET-specific things.
Let's say that C++ code in assembly A has [...]"
(Taken from:
http://www.developer.com/net/cplus/a...919_3367531_1).

What does the author means by assembly? Is this a class?
No.

So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)
Nor will it. It's not C++.
Is this the problem described above, or what does the author means?


You need to ask in a newsgroup dedicated to the language in which your
code is written.

V
Jan 16 '06 #3
So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)


Nor will it. It's not C++.

What's the problem with the code? Maybe I did too much java the last
days... but it looks to me like a correc code snipplet for c++.
Jan 16 '06 #4

"Anton Berg" <no****@gmx.d e> skrev i meddelandet
news:dq******** **@online.de...
So far, I understand this, the following is not possible with
templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)


Nor will it. It's not C++.

What's the problem with the code? Maybe I did too much java the last
days... but it looks to me like a correc code snipplet for c++.


But is it ISO C++ (no), or C++/CLI (perhaps)?

Two totally different languages.
Bo Persson
Jan 16 '06 #5
Anton Berg schrieb:
So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;
should be:

public:
std::vector<int > myVector;
public A(std::vector<i nt> _myVector)
public:
A(std::vector<i nt> myVector_)
{
// do something
}
}
Expecting ';'
class B
{
public std::vector<int > myVector;
public A _a;
see above.
public B() : myVector()
{
_a = new A(myVector);
_a is no pointer.
}
}
;
(code is not compiled!)

Nor will it. It's not C++.


What's the problem with the code? Maybe I did too much java the last
days... but it looks to me like a correc code snipplet for c++.


Is it Java? no! Is it C++? no!

Thomas
Jan 16 '06 #6
Hi,

Thomas J. Gritzan wrote:
Anton Berg schrieb:
Is it Java? no! Is it C++? no!

Ok, sorry! I totally mixed it. Sorry for that and thanks for the hints!
Jan 16 '06 #7
Anton Berg wrote:
So far, I understand this, the following is not possible with templates:
class A
{
public std::vector<int > myVector;

public A(std::vector<i nt> _myVector)
{
// do something
}
}

class B
{
public std::vector<int > myVector;
public A _a;

public B() : myVector()
{
_a = new A(myVector);
}
}

(code is not compiled!)

Nor will it. It's not C++.


What's the problem with the code?


"public" doesn't have a colon after it. The class definition does not
have a semicolon after it. The result obtained from a 'new' expression
has a pointer type and usually cannot be assigned to an object (_a). I
honestly thought you confused this NG as also a place for C#.
Maybe I did too much java the last
days... but it looks to me like a correc code snipplet for c++.


Well, if you have no doubt, you should. It does look like you did just
a bit too much Java. If you have doubt, always try to compile it before
posting, otherwise you cast shadow on your post. Your code, rewritten:

class A
{
std::vector<int > myVector;
public:
A(std::vector<i nt> const & _myVector) : myVector(_myVec tor)
{
}
};

class B
{
std::vector<int > myVector;
A _a;
public:
B() : myVector(), _a(myVector)
{
}
};

And, _yes_, it is perfectly OK, which then brings me to this statement:
I do not understand what you mean "not possible with templates".

V
Jan 16 '06 #8
Hi Victor,
What's the problem with the code?
"public" doesn't have a colon after it. The class definition does not
have a semicolon after it. The result obtained from a 'new' expression
has a pointer type and usually cannot be assigned to an object (_a). I
honestly thought you confused this NG as also a place for C#.

Sorry, for the confussion. Last days I did tooooooo much java, sadly.
And it seems, I was totally too fast with my posting. Sorry for that!

class A
{
std::vector<int > myVector;
public:
A(std::vector<i nt> const & _myVector) : myVector(_myVec tor)
{
}
};

class B
{
std::vector<int > myVector;
A _a;
public:
B() : myVector(), _a(myVector)
{
}
};

And, _yes_, it is perfectly OK, which then brings me to this statement:
I do not understand what you mean "not possible with templates".

Thanks for rewritting my code! I am really sad, that I started to forget
the lovely C++ language...

So, in the article, I mentioned, there the author said, that if I
instantiate a template class in two different assemblys, then I get a
compilation error; that I meant with "not possible". I thought that
assembly is a c++ term I maybe missed or is new to the language (I
didn't do any enhanced c++ stuff for almost 2 years).

Thanks for your help!
Jan 16 '06 #9
Anton Berg wrote:
So, in the article, I mentioned, there the author said, that if
I instantiate a template class in two different assemblys, then
I get a compilation error; that I meant with "not possible". I
thought that assembly is a c++ term I maybe missed or is new to
the language (I didn't do any enhanced c++ stuff for almost 2
years).


I think that by "assembly" the author means "a file containing object
code", i.e., an executable, object, or library file. The point of the
quote you gave is that two such files may have been compiled using
different standard library implementations so that their respective
sources name totally unrelated structures by the same "list<int>"
token sequence. Consequently, linking the modules together such that
one module tries to access an object the other created through a
declaration of its own makes no sense.
Martin

--
Quidquid latine scriptum sit, altum viditur.
Jan 17 '06 #10

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

Similar topics

11
4003
by: andrew queisser | last post by:
I've read some material on the upcoming Generics for C#. I've seen two types of syntax used for constraints: - direct specification of the interface in the angle brackets - where clauses I looked at the files in the Gyro download but I couldn't find any mention of constraints. Can anyone enlighten me what the current status is and what we can expect when Generics are released? Thanks,
9
3033
by: Chuck Bowling | last post by:
I assume that 2.0 will be rolled out with VS.NET 2004. Does anybody know if MS is planning to ship an STL package with it?
1
1437
by: Hatim Ali | last post by:
Hi, I've heard about generics in C#(Whidbey) which is much like templates in C++. As per my understanding, since C++ doesn't have universal base class concept so templates are important. Why templates are important in C# when everything in C# ultimately derives from class "object". For example, I can build a collection class for type "object" and it will work for all the primitive as well as non primitive types since all types are...
2
3106
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
8
3369
by: Chris Dunaway | last post by:
The next version of VS.Net (Whidbey) promises to add generics support to VB. I have a vague remembrance of Templates in C++, but I guess I need a refresher. What will generics allow us to do? How do they make coding easier? Is there a resource that will give me a basic understanding of what generics are and how generics will be useful (in the context of VB.Net)? Thanks
17
1947
by: atgraham | last post by:
Here is the "lead C# architect" attempting to impugn C++ templates (bottom of the page). http://www.artima.com/intv/generics2.html (bottom of the page) (Full article begins at http://www.artima.com/intv/generics.html) I think every employee at Micrsoft must be required to take a class in "FUD marketing" before speaking with anyone outside the company. I think the term "money oriented programming" is suitable here.
2
1276
by: Zoran Stipanicev | last post by:
I'm trying to port simple expression template to generics but I get some strange errors. The code is shown below and the errors are: (1) 'l_' : is not a member of 'IExpresion' (2) syntax error : '(' generic <class Lhs, class Rhs> where Lhs:IExpresion where Rhs:IExpresion ref struct Expression : public DotNetMat::IExpresion
11
2501
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes provide an implemation of the operator +. Now I want to write another generic class Plan<DType>, which can
8
2555
by: Gary Brown | last post by:
Hi, When I do the following public class IAmAGeneric<T> { ... public T AMember (T a, T b) { return a + b; }; }
1
1764
by: titan.nyquist | last post by:
"At the implementation level, the primary difference is that C# generic type substitutions are performed at runtime and generic type information is thereby preserved for instantiated objects." - http://msdn2.microsoft.com/en-us/library/c6cyy67b.aspx Wonder if someone could elaborate this difference to me. I understand performing substitutions at runtime is slow. But, it implies in c++ templates, the type information is not preserved...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10148
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10091
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7499
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3646
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.