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

Home Posts Topics Members FAQ

templated ctor with non-type template argument

I want to write a templated constructor with a non-type template argument,
like this.

class X
{
public:
X() : val(0) {}

template <int I>
X(X const&, X const&) : val(I) {}

private:
int val;
};

This compiles but I have no idea how to 'call' this constructor. Is it
possible?

(Essentially I have a single algorithm to construct an X from two other X's
and I would like to customise that algorithm at compile time using an
integer parameter).

john
Jul 22 '05 #1
2 1247
John Harrison wrote in news:2g******** ****@uni-berlin.de in
comp.lang.c++:
I want to write a templated constructor with a non-type template
argument, like this.

class X
{
public:
X() : val(0) {}

template <int I>
X(X const&, X const&) : val(I) {}

private:
int val;
};

This compiles but I have no idea how to 'call' this constructor. Is it
possible?

No.
(Essentially I have a single algorithm to construct an X from two
other X's and I would like to customise that algorithm at compile time
using an integer parameter).


Wrap the integer up in a type, say boost::mpl::int _<> if you have
boost (*)

eg:

template < int I >
struct int_
{
};

your ctor becomes:

tempalte < int I >
X( X const&, X const&, int_< I > const & /* unused */)
: val( I )
{
}
usage:

int main()
{
X a, b, x( a , b, int_< 1 >() );
}

HTH.

*) boost::mpl -> http://www.boost.org/libs/mpl/doc/

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #2

"Rob Williscroft" <rt*@freenet.co .uk> wrote in message
news:Xn******** *************** ***********@130 .133.1.4...
John Harrison wrote in news:2g******** ****@uni-berlin.de in
comp.lang.c++:
I want to write a templated constructor with a non-type template
argument, like this.

class X
{
public:
X() : val(0) {}

template <int I>
X(X const&, X const&) : val(I) {}

private:
int val;
};

This compiles but I have no idea how to 'call' this constructor. Is it
possible?


No.
(Essentially I have a single algorithm to construct an X from two
other X's and I would like to customise that algorithm at compile time
using an integer parameter).


Wrap the integer up in a type, say boost::mpl::int _<> if you have
boost (*)

eg:

template < int I >
struct int_
{
};

your ctor becomes:

tempalte < int I >
X( X const&, X const&, int_< I > const & /* unused */)
: val( I )
{
}
usage:

int main()
{
X a, b, x( a , b, int_< 1 >() );
}

HTH.


OK, that's clever. Thanks.

john
Jul 22 '05 #3

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

Similar topics

1
1537
by: pmastroianni | last post by:
When a class is instantiated, will the ctor and dtor occupy memory? IF ctor and dtor do occupy memory of an instantiated class where would they be in memeory?
6
1403
by: Klaus Ahrens | last post by:
hi all, acoording to the c++ standard "15.3. - -10- Referring to any non-static member or base class of an object in the handler for a function-try-block of a constructor or destructor for that object results in undefined behavior." it is not allowed to refer to l (delete l;) in the following code
5
3032
by: PasalicZaharije | last post by:
Hallo, few days ago I see ctor like this: Ctor() try : v1(0) { // some code } catch(...) { // some code }
5
1623
by: Grahamo | last post by:
Hi, I have a basic question regarding some legacy code I'm working with; Basically the code looks something like this. I'd like to know if there are any reasons why a particular approach is taken. Given a type X we have a class something like this; class foo
0
2349
by: Mike | last post by:
Hi. I can't figure out why a button's click event is not firing in a templated control I've created (first time I've tried creating one). Please can someone help? On a point of lesser importance, how can I get the designer to realise that my control can contain markup? This is all ASP.Net v 2.0 This is my default.aspx: Below is the source of three files: Default.aspx, Default.aspx.cs and
8
2742
by: Grizlyk | last post by:
Good morning. Look here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/7341aba5238c0f79 and here: http://groups.google.com/group/fido7.ru.cpp.chainik/browse_frm/thread/cb014f4ba9df614a Can anybody answer? Who can't read in russian:
6
1581
by: Richard Thompson | last post by:
Hi - I have a program which was previously working (but wasn't well tested). I've added a new function call, and it now doesn't compile. The call requires a copy constructor, but the compiler appears to think that it actually calls one of the *other* constructors. The copy ctor would work in this context, but the other ctor can't be used (it leads to a template instantiation error, which the compiler is reporting).
2
2166
by: subramanian100in | last post by:
If we do not provide any ctor for a class, the compiler provides the default ctor and copy ctor if needed. Consider a class Test. Suppose we provide some ctor in class Test but do not provide the default ctor. Suppose we try to create Test obj;
6
1533
by: mathieu | last post by:
Hi there, I am currently struggling with a compiler error on a templated code. The error is complete non-sense. I think the issue here is that I am including files in a loop. To handle templates I usually have 3 types of files: - .h : interface + inline function - .cxx : implementation (no templates) - .txx: implementation of templates (with #if blocker to prevent multiple inclusion)
12
1755
by: subramanian100in | last post by:
Can a ctor be declared inline ? Is it not declared inline to avoid executable code size becoming huge ? kindly clarify Thanks V.Subramanian
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
10151
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
10092
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
9950
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
8973
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
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
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
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.