I've found a problem with types defined inside a template. With a
non-template class, I can write the following:
// MyClass.hpp
class MyClass
{
// ...
typedef unsigned int MyType;
MyType MyMethod ( /* ... */ );
// ...
};
// MyClass.cpp
MyClass::MyType
MyClass::MyMeth od ( /* ... */ )
{
// ...
}
This code works well. Of course, I can include a using declaration "using
MyClass::MyType " in MyClass.cpp file, before MyMethod implementation,
instead of specifying the path in its return value.
Well, my problem is that this don't work with templates:
// MyTemplate.hpp
template <typename T>
class MyTemplate
{
// ...
typedef unsigned int MyType;
MyType MyMethod ( /* ... */ );
// ...
};
// MyTemplate.cpp
template <typename T>
MyTemplate <T>::MyType
MyTemplate <T>::MyMethod ( /* ... */ )
{
// ...
}
Is there any alternative way to do this? I've thought to place type
definition outside the class, but this is not correct due to visibility
restrictions (it would make the type visible outside the class, which is not
always desired). Could I include a forward declaration inside the class
declaration (MyTemplate.hpp ), and define it at the beginning of
MyTemplate.cpp?
Thank you very much in advance for your help :-) 3 1628
Ruben Campos wrote: I've found a problem with types defined inside a template. With a non-template class, I can write the following:
// MyClass.hpp class MyClass { // ... typedef unsigned int MyType; MyType MyMethod ( /* ... */ ); // ... };
// MyClass.cpp MyClass::MyType MyClass::MyMeth od ( /* ... */ ) { // ... }
This code works well. Of course, I can include a using declaration "using MyClass::MyType " in MyClass.cpp file, before MyMethod implementation, instead of specifying the path in its return value.
Well, my problem is that this don't work with templates:
// MyTemplate.hpp template <typename T> class MyTemplate { // ... typedef unsigned int MyType; MyType MyMethod ( /* ... */ ); // ... };
// MyTemplate.cpp template <typename T> MyTemplate <T>::MyType MyTemplate <T>::MyMethod ( /* ... */ ) { // ... }
Well - the *definition* for the template method ought to be
in a single header file and you cannot separate the declaration
and definition of templates, as you would do for classes. (
at least not until 'export' is widely implemented. Currently,
there are very few implementations that support that and so,
never bother anyway).
--
Karthik. http://akktech.blogspot.com .
' Remove _nospamplz from my email to mail me. '
"Karthik Kumar" <ka************ *******@yahoo.c om> escribió en el mensaje
news:418b458b$1 @darkstar... Ruben Campos wrote: I've found a problem with types defined inside a template. With a non-template class, I can write the following:
// MyClass.hpp class MyClass { // ... typedef unsigned int MyType; MyType MyMethod ( /* ... */ ); // ... };
// MyClass.cpp MyClass::MyType MyClass::MyMeth od ( /* ... */ ) { // ... }
This code works well. Of course, I can include a using declaration "using MyClass::MyType " in MyClass.cpp file, before MyMethod implementation, instead of specifying the path in its return value.
Well, my problem is that this don't work with templates:
// MyTemplate.hpp template <typename T> class MyTemplate { // ... typedef unsigned int MyType; MyType MyMethod ( /* ... */ ); // ... };
// MyTemplate.cpp template <typename T> MyTemplate <T>::MyType MyTemplate <T>::MyMethod ( /* ... */ ) { // ... }
Well - the *definition* for the template method ought to be in a single header file and you cannot separate the declaration and definition of templates, as you would do for classes. ( at least not until 'export' is widely implemented. Currently, there are very few implementations that support that and so, never bother anyway).
-- Karthik. http://akktech.blogspot.com . ' Remove _nospamplz from my email to mail me. '
You're right. I forgot to say that, in the template case, the .cpp file is
included inside the .hpp file. In fact, when considering optimization with
inline methods, I use a three file (.hpp, .cpp and .inl) scheme like this:
// MyTemplate.hpp
// MyTemplate <T> header file
template <typename T>
class MyTemplate
{
// MyTemplate <T> class declaration ...
};
#ifndef DISABLE_INLINE
#include "MyTemplate.inl "
#endif
#ifndef EXTERNAL_TEMPLA TE_DEFINITION
#include "MyTemplate.cpp "
#endif
// MyTemplate.cpp
// MyTemplate <T> non-inline implementation file
#ifdef EXTERNAL_TEMPLA TE_DEFINITION
#include "MyTemplate .h"
#endif
// MyTemplate <T> non-inline method implementation. ..
#ifdef DISABLE_INLINE
#include "MyTemplate.inl "
#endif
// MyTemplate.inl
// MyTemplate <T> inline implementation file
#ifdef DISABLE_INLINE
#define inline
#endif
// MyTemplate <T> inline method implementation, including the 'inline'
keyword ...
#ifdef DISABLE_INLINE
#undef inline
#endif
I use this scheme for both template and non-template classes. Template
method definitions are really included inside header file while remain
physically in a separate file. So this is not a problem.
"Ruben Campos" <Ru**********@r obotica.uv.es> wrote in message
news:cm******** **@peque.uv.es. .. I've found a problem with types defined inside a template. With a non-template class, I can write the following:
[snip]
First, there are no "source" files for the templates. You will have to
include both template class declaration and definition in where you are
going to instantiate them. Usually you do it either by having a mega
header, or one "master" header that pulls up other files. Eg the master
could contain class declaration and it's last line could have #include of a
cpp "source" looking like file that contains definitions.
Well, my problem is that this don't work with templates:
// MyTemplate.hpp template <typename T> class MyTemplate { // ... typedef unsigned int MyType; MyType MyMethod ( /* ... */ ); // ... };
// MyTemplate.cpp template <typename T> MyTemplate <T>::MyType MyTemplate <T>::MyMethod ( /* ... */ ) { // ... }
This is wrong. You must identify MyType as a type with "typename":
template <typename T>
typename MyTemplate <T>::MyType
MyTemplate <T>::MyMethod ( /* ... */ )
{
// ...
}
With this addition I could compile your code in VS2003
Note that VC6 implements so called "implicit typename", that does not
require you use "typename" in the above example, but it will be an error in
all better standard complying compliers (.net, gnu).
Rgds
ad
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
---------------------------------------------------------- http://www.usenet.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Suzanne |
last post by:
Hi++,
I get a link error when I try to call a template function that is
declared in a header file and defined in a non-header file. I do *not*
get this link error if I define the template function directly in the
header file, or if the change the function to non-template.
For example...
*** Why am I getting a link error for template function f() in the code
below?
|
by: Andy Skypeck |
last post by:
I am looking for some validation against a dubious coding practice
that prevails where I work. C types defined in types.h (Linux) or
stdint.h (Windows, C99?) are used as if they belong to the C++
standard namespace.
std::uint8_t instead of uint8_t
std::uint32_t instead of uint32_t
....
I don't think the use of std:: is correct. Nowhere are these types
|
by: Chris Foster |
last post by:
Hi, I'm having some difficulty using types which are defined in a base
class inside a derived class. The problem crops up using template
classes.
The following test code encapsulates what I'd like to do, but doesn't
compile with g++ (v3.3.3).
//---------------------------------------------------------
// A = base class
template <typename T>
|
by: andrew |
last post by:
Hi,
I'm a C++ newbie, so apologies for this rather basic question. I've
searched for help and, while I understand the problem (that the outer
class is not yet defined), I don't understand what the "correct"
solution would be.
I am trying to model lists much like in Lisp, using a "Cons" object to
hold two pointers - one to an Element, and the other to a further Cons.
Cons is a subclass of Element, so lists can be nested.
|
by: GrahamJWalsh |
last post by:
I have a basic question regarding various data types (related to
reinterpret_cast, static_cast).
Lets say I have some declarations thus;
int i = 444;
unsigned int* uip;
I then do something like;
| |
by: lovecreatesbeauty |
last post by:
Prof. Bjarne Stroustrup said "built-in data types have default
constructor" at §10.4.2 in `TC++PL, special ed.'.
He also said that "built-in data types are not classes" at §11.4 in
`TC++PL, special ed.'.
(Sorry, I'm reading a Chinese edition of the book, and can't get a
English edition handy currently. Perhaps these words are not same as
his book, but I think the meaning is the same.)
|
by: No One |
last post by:
Here is my problem:
I have a certain set of well-defined manipulations that I have to apply
to different types of data. In all cases the manipulations are exactly the
same, and are to be performed on the different types of data. Currently I
have a collection of functions that do exactly the same - the only
difference between them is the type of data they act on. Let me present a
toy example:
I have the following two data types:
|
by: Herby |
last post by:
I need to define my own types and arrays of these types.
These types are for the most part extensions of the built in types and
need to provide all the basic operations of arithmetic and relational.
If i was using C++ I would overload the operators against my types and
use STL Vector etc.
No problem. No performance costs. No virtual functions, no casting
etc.
|
by: lobequadrat |
last post by:
Hello,
I am trying to get the following code work (unfortunately not
mine ... :( )
template <class Tclass Test {
public:
class ELEM;
|
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,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |