473,386 Members | 2,129 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,386 software developers and data experts.

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<int> _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 2326

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<int> _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<int> _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<int> _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.de> 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<int> _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<int> _myVector)
public:
A(std::vector<int> 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<int> _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<int> const & _myVector) : myVector(_myVector)
{
}
};

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<int> const & _myVector) : myVector(_myVector)
{
}
};

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
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...
9
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
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...
2
by: Mr.Tickle | last post by:
So whats the deal here regarding Generics in the 2004 release and templates currently in C++?
8
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? ...
17
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...
2
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 :...
11
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...
8
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
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." -...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.