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

Home Posts Topics Members FAQ

template constructor in non-template class

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};

class Butcher
{
public:
template <Method m>
Butcher();
private:
Vector a, b, c;
};

template <>
Butcher::Butche r<A>()
: a(2), b(4), c(2)
{
a = 0.5, 1;
b = -1, 1, 3, 2;
c = 0, 1;
}

....

I do not want to call a template function from a normal constructor because
I would have to allocate the vectors twice. Is there a way. I think the
trouble is that constructors are unnamed and I have no argument, from which
the template parameter value can be derived.

regards,
alex
Jul 22 '05 #1
2 5006
On Wed, 01 Dec 2004 11:39:47 +0100, Alexander Stippler
<st**@mathemati k.uni-ulm.de> wrote:
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?


No. A workaround is to add a dummy argument of the type

template <Method m>
class MethodWrapper{} ;

or similar to the constructor.

Tom
Jul 22 '05 #2
On Wed, 01 Dec 2004 13:21:03 +0000, Tom Widmer
<to********@hot mail.com> wrote:
On Wed, 01 Dec 2004 11:39:47 +0100, Alexander Stippler
<st**@mathemat ik.uni-ulm.de> wrote:
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?


No. A workaround is to add a dummy argument of the type

template <Method m>
class MethodWrapper{} ;

or similar to the constructor.


Or you could create a templated factory function instead of templating
the constructor.

Tom
Jul 22 '05 #3

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

Similar topics

1
1747
by: Adam | last post by:
Why does this compile: if (err != Socket::errSuccess) { switch (err) { case Socket::errTimeout: ConnectionIOException cio(new string(myTCPStream->getErrorString()), ConnectionIOException::TIMEOUT); throw cio; default:
34
2079
by: Jason Heyes | last post by:
Is there a special name for a constructor that does struct-like initialisation? Example: class DefaultCtor { std::string class_name; bool compiler_generated; public: DefaultCtor(std::string class_name, bool compiler_generated);
10
2502
by: Steve | last post by:
Hi, I thought I had invented a pretty neat solution for my problem, but it turns out I'm using the named constructor idiom. :-) I've seen examples where empty structs are used to identify the different constructors by their first parameter: struct Type1 {}; struct Type2 {};
2
2225
by: vakap | last post by:
function show() { var s = '' ; for (var i = 0; i<arguments.length; s += '\n'+arguments) ; typeof(window) != 'undefined' ? window.alert(s) : WScript.Echo(s) ; } function f(){} show('delete(f):',delete(f)) ; // false g = function(){} ;
3
4179
by: Marcus Kwok | last post by:
Is it in the standard that one cannot declare a member of a struct that uses a non-default constructor? Here is my simplified code; the Matrix class was taken from the FAQ, and works well otherwise (error checking was removed for simplicity). #include <iostream> #include <vector> const int NUM_SEASONS = 4;
24
3638
by: rdc02271 | last post by:
Hello! Is this too crazy or not? Copy constructor: why can't I copy objects as if they were structs? I have a set of simple objects (no string properties, just integers, doubles) and I have to copy the same object millions of times. So instead of writing in the copy constructor property1=SourceObject.property1 can't I use memory copy functions to do this faster? Is this too stupid? By the way, I'm a C++ newbie! But don't go easy on me...
4
1865
by: fcvcnet | last post by:
Hi, If we do not provide the default constructor(non-parameter constructor), the compiler will create one for us. If we have a constructor with parameter and we do not provide the non-parameter constructor, wether the compiler create default constructor for us? Thank you very much.
74
16040
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the creation of this implicit default constructor, to force the creation of a struct via my constructor only? Zytan
13
2480
by: Jeroen | last post by:
Hi all, I'm trying to implement a certain class but I have problems regarding the copy ctor. I'll try to explain this as good as possible and show what I tried thusfar. Because it's not about a certain code syntax but more a 'code architecture' thing , I'll use simple example classes (which are certainly not complete or working...) just to illustrate the idea (and I may make some mistakes because I'm not that experienced...). The...
10
1912
by: Jason Doucette | last post by:
Situation: I have a simple struct that, say, holds a color (R, G, and B). I created my own constructors to ease its creation. As a result, I lose the default constructor. I dislike this, but it's easy to solve: I just make my own default constructor. Problem: My own default constructor is considered to be *initializing the variable* (even though it doesn't), whereas the original one does not. Thus, when I declare and use it before...
0
10561
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, 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...
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...
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...
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...
1
4277
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
2
3803
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2976
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.