template function construcor

  • Thread starter Mike -- Email Ignored
  • Start date
M

Mike -- Email Ignored

In a class that is not a template class, can one
of the constructors be a template function?

Thanks,
Mike.
 
I

Ian Collins

Mike said:
In a class that is not a template class, can one
of the constructors be a template function?

Yes. Concrete classes can have template member functions and template
constructors.
 
A

Alf P. Steinbach

* Mike -- Email Ignored:
In a class that is not a template class, can one
of the constructors be a template function?

Just to add to Ian's "yes", there are a few subtleties to be aware of.

The most important is that

* A templated constructor is never a copy constructor.

class Blah
{
private:
template< typename T > Blah( T const& ) {}
public:
Blah() {}
};

int main()
{
Blah o = Blah(); // OK, uses generated copy constructor.
}

A second subtlety is that formally a C++ constructor has no name. And that means
that the only way you can specify a constructor template parameter is
indirectly, via the ordinary arguments, so it's no good having some template
parameter that isn't used in some formal argument type. When all that matters is
the type you might use a dummy pointer argument.

A third subtlety is that a templated constructor might be the best match where
you really want to call some other constructor. One solution is then to provide
a lot of concrete constructors for various types. Another solution, perhaps used
in conjunction with the first, is to apply some Boost enable_if. But the best
solution is to see whether you can eliminate the templating. Because templating
just causes a lot of subtle problems that can be compiler-specific.


Cheers & hth.,

- Alf
 
M

Mike -- Email Ignored

]
Cheers & hth.,

- Alf

This is useful information. In my case, an argument can by any
of the unsigned integer types. AFAICS, there should be no
problem, your admonitions notwithstanding. Please let me know
if you disagree.

Thanks,
Mike.
 
I

Ian Collins

Mike said:
]
Cheers & hth.,

- Alf

This is useful information. In my case, an argument can by any
of the unsigned integer types. AFAICS, there should be no
problem, your admonitions notwithstanding. Please let me know
if you disagree.

What are you going to do with the argument?
 
J

James Kanze


This is useful information. In my case, an argument can by
any of the unsigned integer types. AFAICS, there should be no
problem, your admonitions notwithstanding. Please let me know
if you disagree.

What's the problem with just having a single constructor which
takes an unsigned long long? This sounds like a case where
templates are NOT the answer.
 
M

Mike -- Email Ignored


What's the problem with just having a single constructor which takes an
unsigned long long? This sounds like a case where templates are NOT the
answer.

I was doing a computation that involved sizeof(TYP),
but it may not be necessary, in which case uint64_t
would be good.

If both would work, what is the downside of using
a template?

BTW, is unsigned long long defined as uint64_t,
or might it change?

Mike.
 
I

Ian Collins

Mike said:
What's the problem with just having a single constructor which takes an
unsigned long long? This sounds like a case where templates are NOT the
answer.

I was doing a computation that involved sizeof(TYP),
but it may not be necessary, in which case uint64_t
would be good.

If both would work, what is the downside of using
a template?

BTW, is unsigned long long defined as uint64_t,
or might it change?

That depends on the implementation. In C long long is required to be at
lest (not exactly) 64 bits.
 
J

James Kanze

What's the problem with just having a single constructor
which takes an unsigned long long? This sounds like a case
where templates are NOT the answer.
I was doing a computation that involved sizeof(TYP),
but it may not be necessary, in which case uint64_t
would be good.
If both would work, what is the downside of using
a template?

Unnecessary additional complexity, and in the absense of export,
increased coupling.
BTW, is unsigned long long defined as uint64_t,
or might it change?

It's a basic type, so it isn't defined as anything else.
(uint64_t might be defined as unsigned long long.) The standard
requires it to have at least 64 value bits, but it can have
more---there is at least one system where it has 72 bits, for
example.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,886
Members
47,419
Latest member
ArturoBres

Latest Threads

Top