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

Exposing template instances from managed c++ dll

If I compile the following code in my managed c++ dll...

public ref class foo
{
public:
void doSomething() { }
};
public ref class bar { };

and then inspect the resulting dll using ildasm... I can see that foo
and bar both have a function called doSomething.

However, if foo is a template and bar inherits from it, e.g.

template <typename t>
public ref class foo
{
public:
void doSomething() { }
};
public ref class bar : public foo<System::String{ };

Then both bar and the foo template instance do not have the doSomething
function according to ildasm (and my client assemblies).

What am I doing wrong? thanks.

Sep 24 '06 #1
7 1653
Hi,

"cortfr" <co****@gmail.comwrote in message
news:11*********************@m73g2000cwd.googlegro ups.com...
If I compile the following code in my managed c++ dll...

public ref class foo
{
public:
void doSomething() { }
};
public ref class bar { };

and then inspect the resulting dll using ildasm... I can see that foo
and bar both have a function called doSomething.

However, if foo is a template and bar inherits from it, e.g.

template <typename t>
public ref class foo
{
public:
void doSomething() { }
};
public ref class bar : public foo<System::String{ };

Then both bar and the foo template instance do not have the doSomething
function according to ildasm (and my client assemblies).

What am I doing wrong? thanks.
Templates are a C++ only feature so you AFAIK you cannot expose template
classes to other .Net languages.
Use generic instead which gives you some of the functionality you have with
template:

generic<typename T>
public ref class foo...

--
SvenC
Sep 25 '06 #2
Hi Sven,
Thanks for the reply. Generics won't work for what I'm trying to do,
mainly because the template parameter I want to use is not a managed
class. I used a managed class (System::String) in the example above
just to simplify the question.

Anyways, I can understand why the template foo isn't exposed and usable
from other .NET languages... but I still don't understand why the
template instance (i.e. foo<System::String>) is not exposed. The
template instance should just be as if I created another class myself
by copying foo and replacing all instances of T with System::String,
right?

Templates are a C++ only feature so you AFAIK you cannot expose template
classes to other .Net languages.
Use generic instead which gives you some of the functionality you have with
template:

generic<typename T>
public ref class foo...

--
SvenC
Sep 25 '06 #3
cortfr wrote:
If I compile the following code in my managed c++ dll...

public ref class foo
{
public:
void doSomething() { }
};
public ref class bar { };

and then inspect the resulting dll using ildasm... I can see that foo
and bar both have a function called doSomething.

However, if foo is a template and bar inherits from it, e.g.

template <typename t>
public ref class foo
{
public:
void doSomething() { }
};
public ref class bar : public foo<System::String{ };

Then both bar and the foo template instance do not have the
doSomething function according to ildasm (and my client assemblies).

What am I doing wrong? thanks.
Try this:

template <typename t>
public ref class foo
{
public:
virtual void doSomething() { }
};
public ref class bar : public foo<System::String{
public:
virtual void doSomething() override { }
};

-cd
Sep 25 '06 #4
Ah right... thanks Carl... just setting foo's doSomething to be virtual
allows doSomething to be exposed. So that solves my problem. But it
still seems odd that you'd have to do that.
Carl Daniel [VC++ MVP] wrote:
cortfr wrote:
If I compile the following code in my managed c++ dll...

public ref class foo
{
public:
void doSomething() { }
};
public ref class bar { };

and then inspect the resulting dll using ildasm... I can see that foo
and bar both have a function called doSomething.

However, if foo is a template and bar inherits from it, e.g.

template <typename t>
public ref class foo
{
public:
void doSomething() { }
};
public ref class bar : public foo<System::String{ };

Then both bar and the foo template instance do not have the
doSomething function according to ildasm (and my client assemblies).

What am I doing wrong? thanks.

Try this:

template <typename t>
public ref class foo
{
public:
virtual void doSomething() { }
};
public ref class bar : public foo<System::String{
public:
virtual void doSomething() override { }
};

-cd
Sep 25 '06 #5
"cortfr" <co****@gmail.comwrote in message
news:11**********************@b28g2000cwb.googlegr oups.com...
Ah right... thanks Carl... just setting foo's doSomething to be virtual
allows doSomething to be exposed. So that solves my problem. But it
still seems odd that you'd have to do that.
Virtual functions have to have their address taken and placed in the
v-table. So that creates a reference to the function, and it can't be
optimized out.
>

Carl Daniel [VC++ MVP] wrote:
>cortfr wrote:
If I compile the following code in my managed c++ dll...

public ref class foo
{
public:
void doSomething() { }
};
public ref class bar { };

and then inspect the resulting dll using ildasm... I can see that foo
and bar both have a function called doSomething.

However, if foo is a template and bar inherits from it, e.g.

template <typename t>
public ref class foo
{
public:
void doSomething() { }
};
public ref class bar : public foo<System::String{ };

Then both bar and the foo template instance do not have the
doSomething function according to ildasm (and my client assemblies).

What am I doing wrong? thanks.

Try this:

template <typename t>
public ref class foo
{
public:
virtual void doSomething() { }
};
public ref class bar : public foo<System::String{
public:
virtual void doSomething() override { }
};

-cd

Sep 25 '06 #6
Ben, See my first post for context. Note that doSomething in foo does
NOT have to be virtual in order for bar to expose that function. But
if foo is a template, then it does have to be virtual.
thanks.
Virtual functions have to have their address taken and placed in the
v-table. So that creates a reference to the function, and it can't be
optimized out.
Sep 25 '06 #7
"cortfr" <co****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
Ben, See my first post for context. Note that doSomething in foo does
NOT have to be virtual in order for bar to expose that function. But
if foo is a template, then it does have to be virtual.
thanks.
>Virtual functions have to have their address taken and placed in the
v-table. So that creates a reference to the function, and it can't be
optimized out.
Ok, good point. Still, template instantiation is driven by use, so the
v-table is considered a reference to the function and the compiler has to
generate it.

Non-template functions would normally be removed by the optimizer (in the
link stage), however exported functions are "used" by the export table
(native) or metadata (managed) and can't be removed. However templates
can't be exported...
Sep 25 '06 #8

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

Similar topics

4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
4
by: Peter Hemmingsen | last post by:
Hi, I have a dotnet object (implemented in mc++ and used in c#) which have a property called "Info". The Info property is also a dotnet object (implemented in mc++). In the constructor of the...
0
by: Daniel G?rsch | last post by:
The following code works perfect in VS 2002 (compiled in a console project). Using VS 2003 the variable 'a' in the 'compute' function of the managed class 'M' is not correctly passed into this...
6
by: Hendrik Schober | last post by:
Hi, I have a problem with extending some existing code. In a simplified form, the problem looks like this: I have four types, A, B, C, and D. Each A refers to zero, one, or more B's and each...
3
by: Stormy | last post by:
Can I define a template class as following #pragma unmanaged #include "mybaseclass.h" #pragma managed namespace mymanaged1 { namespace mymanaged2 { namespace myunmanaged
8
by: Imre | last post by:
Hi I'm looking for a way to make sure that whenever a new instance of a class template A is created, then an instance of class template B is also created, with the same template parameters. Of...
4
by: AndrewD | last post by:
Hey C++ folks, I created this today, just for fun. You can make object allocation for any class around 6 times faster, simply by doing the following. class MyClass : public...
4
by: Alex | last post by:
Hi, everybody, I found some simple example of writing of a service application in managed C++. The only thing my service has to do is periodically to call some COM Server function written in...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.