473,790 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

template & specialization

I am trying to write two constructors for the same class. One takes an
iterator and so is a template. The other takes a particular type by
reference to const.

class Foo {
public:
template<typena me InputIterator>
Foo(InputIterat or i);
Foo(const Bar & b);

When I do this, trying to construct a Foo from a Bar calls the
template, not the constructor from a Bar. This is in VC++ 7.0. And it
does not matter which I declare first.

Questions: Is this a compiler oddity or standard behavior? Is there any
way around it?

I know a semi-solution. When I pass them both by value or both by
reference to const, it works.

template<typena me InputIterator>
Foo(InputIterat or i);
Foo(Bar b);

or

template<typena me InputIterator>
Foo(const InputIterator & i);
Foo(const Bar & b);

But I would prefer not to do that. Any other ideas/thoughts?

May 4 '06 #1
2 1852
Glenn G. Chappell wrote:
I am trying to write two constructors for the same class. One takes an
iterator and so is a template. The other takes a particular type by
reference to const.

class Foo {
public:
template<typena me InputIterator>
Foo(InputIterat or i);
Foo(const Bar & b);

When I do this, trying to construct a Foo from a Bar calls the
template, not the constructor from a Bar. This is in VC++ 7.0. And it
does not matter which I declare first.

Questions: Is this a compiler oddity or standard behavior? Is there
any way around it?
It is a standard behaviour. If you construct from an object, the template
one will win because the compiler will figure out what type it is from
the argument and there will be no conversion required. A const reference
requires "binding to".
I know a semi-solution. When I pass them both by value or both by
reference to const, it works.

template<typena me InputIterator>
Foo(InputIterat or i);
Foo(Bar b);

or

template<typena me InputIterator>
Foo(const InputIterator & i);
Foo(const Bar & b);

But I would prefer not to do that. Any other ideas/thoughts?


You can add a fake argument to the one you don't care to keep implicit:

template<typena me InputIterator>
Foo(InputIterat or i, int);

Of course, you will have to stick an extra argument when constructing
an object from the iterator...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 4 '06 #2
* Glenn G. Chappell:
I am trying to write two constructors for the same class. One takes an
iterator and so is a template. The other takes a particular type by
reference to const.

class Foo {
public:
template<typena me InputIterator>
Foo(InputIterat or i);
Foo(const Bar & b);

When I do this, trying to construct a Foo from a Bar calls the
template, not the constructor from a Bar. This is in VC++ 7.0. And it
does not matter which I declare first.
With both MSVC 7.1 and MingW g++ 3.4.2, the following program,

#include <iostream> // std::cout
#include <ostream> // operator<<, std::endl

void say( char const s[] ) { std::cout << s << std::endl; }

class Bar {};

class Foo
{
public:
template< typename InputIterator >
Foo( InputIterator ) { say( "Template" ); }

Foo( Bar const& ) { say( "Non-template" ); }
};

int main()
{
Bar b;
Foo f( b );
}

says "Non-template", but when passed an argument of type derived from
Bar it says "Template".

Questions: Is this a compiler oddity or standard behavior?
Off-hand I'd say a compiler oddity for the case of pure Bar argument.

Is there any way around it?
Best, use the solution you outline below. Second best (but should be
done anyway), upgrade the compiler. Third, perhaps use SFINAE
(Substitution Failure Is Not An Error).

The Boost library has a ready-made SFINAE solution. The following might
give you an idea if you don't want to use Boost:

#include <iostream> // std::cout
#include <ostream> // operator<<, std::endl

void say( char const s[] ) { std::cout << s << std::endl; }

class Bar {};
class BarD: public Bar {};

template< typename InputIterator >
struct NonBar { typedef InputIterator T; };

template<> struct NonBar<Bar>;
template<> struct NonBar<BarD>;

class Foo
{
public:
template< typename InputIterator >
Foo( InputIterator, typename NonBar<InputIte rator>::T* = 0 )
{ say( "Template" ); }

Foo( Bar const& ) { say( "Non-template" ); }
};

int main()
{
Bar b;
BarD bd;

Foo f( b );
Foo fd( bd );
Foo fi( 666 );
}

with output "Non-template, non-template, template" with both MSVC 7.1
and MingW g++ 3.4.2.

Of course to make it more general you'd have to check sub-subclass
relationship and somehow make that part of the SFINAE checking (not sure
whether that's possible, perhaps the Boost solution does that), and
anyway, as I wrote above, consider solutions (1) and (2) first.

Solution (1):

I know a semi-solution. When I pass them both by value or both by
reference to const, it works.

template<typena me InputIterator>
Foo(InputIterat or i);
Foo(Bar b);

or

template<typena me InputIterator>
Foo(const InputIterator & i);
Foo(const Bar & b);

But I would prefer not to do that. Any other ideas/thoughts?


I think this is the cleanest solution.

Why do you want to avoid this?
--
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?
May 4 '06 #3

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

Similar topics

17
6868
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section 18.3.1, 'Iseq'). I want the version for containers that he gives, but also to provide a specialization for construction from a pair<It,It> (eg because that is returned by equal_range()).
2
2544
by: SainTiss | last post by:
Hi, If you've got a template class with lots of methods, and then you've got a type which works with the template, except for one method... What you need to do there is specialize the template for your type. However, this requires you to copy the whole template, and change the method, which leads to code duplication... If there's only 1 template parameter, one can specialize the method
8
7689
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I specialize Music<Bach>, I expect that the original play() method is available in the specialization, but it is not. How can I fix this? -X
2
5780
by: Jeff | last post by:
/* -------------------------------------------------------------------------- Hello, I was experimenting with class templates and specializing member functions and came across a simple problem which I can't figure out. I have a simple class C with 2 template member objects, and a function print() that prints the value of these objects. I defined a specialization of print() for C<string, char> which is called correctly. Then I wanted...
3
3975
by: Nathanael D. Noblet | last post by:
Hello, I'm having a problem with a templated class. I have two template classes Node and List. Everything works fine when the type for List is an object and not a pointer ie:"List<Object> theList". It works when it is a pointer as well the problem is in a memory leak it causes. For example an example of the problem it causes see the code below. <snip>
6
2020
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never be instantiated. Now consider this explicit instantiation of foo:
6
1776
by: NKOBAYE027 | last post by:
FIRST POST Hi All: I'm trying to write a simple specialization before moving on to something a bit more complex - always a good idea in my case, at least. :o) I'm trying to adapt the example from Stroustrup, 3rd ed., The C++ Programming Language p. 344 I'm using MSDev 6.0 in case that's an issue. Here's the source...
9
2785
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second assign() function really a specialization of the first assign() or is it an assign() overload? Thank you. -- Marek
2
7702
by: Barry | last post by:
The following code compiles with VC8 but fails to compiles with Comeau online, I locate the standard here: An explicit specialization of any of the following:
6
2620
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass Indexer<std::vector<T,A {} matches only with nonconst version. anyway to match it for both? and get if it is const or nonconst? Actually i want 2 specialization, one for std::vector<T,Aconst & non const other for std::deque<T,Aconst & non const.
0
9666
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
10413
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...
1
10145
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
9986
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
9021
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
5422
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
5551
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4094
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
2909
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.