473,804 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template parameter type constructor method call

How do I write a constructor mehtod call in this case

/*-----------*/
template<typena me Tclass CObjectPoolImpl
{
public:

void smth(T* pObj)
{
if (pObj)
pObj->T::T(); // an attempt to call a constructor method of
class T
}

};

CObjectPoolImpl <mynamespace::C MyTypeCF;
CMyType mt;

CF.smth(&mt);
/*-----------*/

MS Visual C++ 7.1:
error C2039: 'T' : is not a member of 'mynamespace::C MyType'

There is a reason not to write 'new T' and not to explicitly write
constructor method name (type name). Default constructor method for
CMyType exists.

Oct 12 '06
18 3668
Victor Bazarov posted:
Probably not including <memory>
You sure you don't mean <new>?

--

Frederick Gotham
Oct 12 '06 #11
AlexanderVX wrote:
Kai-Uwe Bux, thanks, it removes unwanted assignment but compilation
error does not go away...
Well, the line is legal. I would guess that there is a syntax error
somewhere close. Please post a minimal complete example that shows the
problem.
Best

Kai-Uwe Bux
Oct 12 '06 #12
Frederick Gotham wrote:
Victor Bazarov posted:
>Probably not including <memory>

You sure you don't mean <new>?
Do we need any headers for placement new? I thought, <newjust provides
allocation and deallocation functions. Placement new shouldn't need those,
or would it?
Best

Kai-Uwe Bux
Oct 12 '06 #13
* Kai-Uwe Bux:
Frederick Gotham wrote:
>Victor Bazarov posted:
>>Probably not including <memory>
You sure you don't mean <new>?

Do we need any headers for placement new? I thought, <newjust provides
allocation and deallocation functions. Placement new shouldn't need those,
or would it?
There are infinitely many different placement new's, but /the/ placement
new, the one that constructs in place, requires the header <new>. It's
also a good idea to use '::'. This is because it's a library feature,
not a language feature (although those are strongly connected here).

But, but, but... I hear you say, the <newheader also defines the
standard global allocation function invoked by ordinary 'new', and we
don't need to include no flickin' <newheader to use ordinary 'new'?

Well, that's because the ordinary 'new' is made easy to use, while the
placement form is made less easy to use. It's specified by §5.3.4/11.
If the placement new syntax is used (and only then), overload resolution
is performed to determine the allocation function to call, which
allocation function, in the case of /the/ placement new, is supplied by
the <newheader.

Cheers,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Oct 12 '06 #14
Kai-Uwe Bux wrote:
Frederick Gotham wrote:
>Victor Bazarov posted:
>>Probably not including <memory>

You sure you don't mean <new>?

Do we need any headers for placement new? I thought, <newjust
provides allocation and deallocation functions. Placement new
shouldn't need those, or would it?
He's right, <newis what's needed, not <memory>. Placement new
is declared in it. See 18.4 of the Standard.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 12 '06 #15
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
>Frederick Gotham wrote:
>>Victor Bazarov posted:

Probably not including <memory>

You sure you don't mean <new>?

Do we need any headers for placement new? I thought, <newjust
provides allocation and deallocation functions. Placement new
shouldn't need those, or would it?

He's right, <newis what's needed, not <memory>. Placement new
is declared in it. See 18.4 of the Standard.
Thanks, now that I read it, I feel that the whole thing is a mess. Do I
understand this correctly:

1. Any call to new (placement or not) will call an allocation function and
construct the object.

2. For the usual placement new, we do not want to allocate memory; thus, we
provide a special allocation function that intentionally does nothing. When
allocation is a null-op we are just left with the construction of the
object that any call to new will perform.

3. To add insult to injury, we need to include the header <newbecause that
is where the 'allocation function that intentionally does nothing' is
declared.

Oh boy: I need to include the right header to get a function that does
nothing! Why does it have to be that weird? More and more, I come to think
that we should just be able to call constructors as we can call the
destructor.
Thanks again

Kai-Uwe Bux
Oct 13 '06 #16
Kai-Uwe Bux wrote:
[..] Do
I understand this correctly:

1. Any call to new (placement or not) will call an allocation
function and construct the object.

2. For the usual placement new, we do not want to allocate memory;
thus, we provide a special allocation function that intentionally
does nothing. When allocation is a null-op we are just left with the
construction of the object that any call to new will perform.

3. To add insult to injury, we need to include the header <new>
because that is where the 'allocation function that intentionally
does nothing' is declared.
I am not sure how you make this conclusion. The header <newdeclares
overloaded 'operator new' functions that take some special arguments
(along with the size). Those forms do not exist by themselves in the
language, they are part of the library. It really has nothing to do
whether the allocation function for placement new does anything.
>
Oh boy: I need to include the right header to get a function that does
nothing!
No, not at all. You need to include the right header for the right
'operator new' function declaration.
Why does it have to be that weird? More and more, I come to
think that we should just be able to call constructors as we can call
the destructor.
It's all in your head.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 13 '06 #17
Well, the line is legal. I would guess that there is a syntax error
somewhere close. Please post a minimal complete example that shows the
problem.
In headers I found this... Left by my predeccessor a million years
ago...

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

I removed it and the proposed "placement new" solution works!

Oct 13 '06 #18
Victor Bazarov wrote:
Kai-Uwe Bux wrote:
>[..] Do
I understand this correctly:

1. Any call to new (placement or not) will call an allocation
function and construct the object.

2. For the usual placement new, we do not want to allocate memory;
thus, we provide a special allocation function that intentionally
does nothing. When allocation is a null-op we are just left with the
construction of the object that any call to new will perform.

3. To add insult to injury, we need to include the header <new>
because that is where the 'allocation function that intentionally
does nothing' is declared.

I am not sure how you make this conclusion. The header <newdeclares
overloaded 'operator new' functions that take some special arguments
(along with the size). Those forms do not exist by themselves in the
language, they are part of the library. It really has nothing to do
whether the allocation function for placement new does anything.
I didn't (mean to) say that you need <new*only* to include functions that
don't do anything or that whether you have to include <newdepends on
whether the functions do something or not.

What I find strange is the round-about way of C++ to provide object
construction at a specified location. Here is what I figured from 5.3.4:

A placement new expression like

new ( some, args ) T ( some, other, args )

can be understood as performing two steps (given in pseudo-code):

1. void* pos = operator new( sizeof(T), some, args ).
2. construct T( some, other, args ) at pos.

Thus, placement new provides a hook to supply additional arguments to an
allocation function (5.3.4/11).

As a trick, for just constructing an object of a given location, the header
<newprovides the overload

void* operator new (std::size_t size, void* ptr) throw();

described in 18.4.1.3. And this operator new overload is deliberately
designed to just return ptr and perform no other action. Thus, if you do

new ( (void*) t_ptr ) T ( some, other args );

the above two steps translate to

1. void* pos = operator new( sizeof(T), (void*)t_ptr );
( equivalent to: void* pos = (void*) t_ptr; )
2. construct T( some, other, args ) at pos.

I consider that a pretty awkward way of going about construction of an
object. The compiler knows how to construct an object at a given location
anyway. This is nothing that is done by the operator new overload from
<new>; and in fact, one would expect (or hope) that step 1 in the above
sequence is optimized away. So, I find it strange that C++ does not offer a
way of constructing an object at a given location that does not involve an
allocation function to be imported from <new>.

It's like using delete with the deallocation function from 18.4.1.3/5-10 for
calling a destructor.

>Oh boy: I need to include the right header to get a function that does
nothing!

No, not at all. You need to include the right header for the right
'operator new' function declaration.
Yes, and 18.4.1.3/1-4 describes the right one for the line in question

new ( (void*) t_ptr ) T ( some, other args );

and this right one just returns its second argument and does nothing else.

>Why does it have to be that weird? More and more, I come to
think that we should just be able to call constructors as we can call
the destructor.

It's all in your head.
I would think, it was in the standard before it was in my head :-)
Best

Kai-Uwe Bux
Oct 14 '06 #19

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

Similar topics

2
5006
by: Alexander Stippler | last post by:
Hello, I want to write a non-template class with a constructor template member. The constructor shall not take arguments. Is this possible at all? Here the concrete example: A class which contains three vectors. There are several different methods to initialize these vectors(size and contents). These methods are the template parameter I want to use. enum Method {A, B, C, D, E, F};
8
2248
by: Alexander Stippler | last post by:
Hello, short question: What is illegal about the following code? template <typename T> class Method { }; class Procedure
14
2908
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&); template<typename T>
8
3157
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int, 1> and A<int, 2> are different types. Now, if the A template
12
2628
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with the appropriate factory: // In some header file... #include <cassert>
12
2331
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include <iostream> using namespace std; template <class ClassB> class ClassA
3
2038
by: mike.arsenault | last post by:
Hello I need some help from anyone that can provide it. Below is a function inside a template collection class that I'm writing. I have a TYPE * that points to an allocated memory location, and I'm explicitly trying to call the TYPE::TYPE() constructor on it by using the pointer. That doesn't compile, yet if I forget the usage of TYPE and write the line to use the actual constructor, then it's fine. See the code below for an example.
0
1247
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
3
3585
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto be instantiated. The user does not have
0
9571
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
10318
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
10302
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,...
0
10069
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 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...
0
9132
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, 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...
1
7608
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
6845
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();...
0
5505
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...
0
5639
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.