boost::enable_if and constructors

  • Thread starter Christoph Bartoschek
  • Start date
C

Christoph Bartoschek

Hi,

I try to use boost::enable_if inside of a constructor but the following code
fails. How can I correct it?


#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>


template <typename T>
struct Test {
Test() {}

template <typename U>
Test(typename boost::enable_if<
>::type )
{}


};

class Base {}; class Derived : public Base {};

int main() {

Test<Derived> d;
Test<Base> hb(d);
}

Greetings
Christoph Bartoschek
 
A

Alf P. Steinbach

* Christoph Bartoschek:
I try to use boost::enable_if inside of a constructor but the following code
fails. How can I correct it?


#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_base_and_derived.hpp>


template <typename T>
struct Test {
Test() {}

template <typename U>
Test(typename boost::enable_if<

{}


};

class Base {}; class Derived : public Base {};

int main() {

Test<Derived> d;
Test<Base> hb(d);
}

First, please see the FAQ item titled "How do I post a question about
code that doesn't work correctly?", currently at <url:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.8>.

For example, "fails" is not exactly a precise description of the problem.

Having said that, the problem is simply that the compiler can't deduce
the template parameter type. From the compiler's point of view the type
specification is a one-way cryptographic function, sort of, which it's
unable to unscramble. Instead of using enable_if directly as the type
of the formal argument, add a dummy pointer argument using enable_if.
 
C

Christoph Bartoschek

Alf said:
Having said that, the problem is simply that the compiler can't deduce
the template parameter type. From the compiler's point of view the type
specification is a one-way cryptographic function, sort of, which it's
unable to unscramble. Instead of using enable_if directly as the type
of the formal argument, add a dummy pointer argument using enable_if.

Thanks for your answer. I will go the dummy argument way, however I wonder
whether there is a runtime penalty by using such an unused parameter.

Greetings
Christoph Bartoschek
 
D

David Harmon

On Sun, 03 Dec 2006 22:01:32 +0100 in comp.lang.c++, Christoph
Bartoschek said:
Thanks for your answer. I will go the dummy argument way, however I wonder
whether there is a runtime penalty by using such an unused parameter.

A guess: probably zero penalty since Test::Test() is inline.
 

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
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top