E
E
Elle Solomina2013-07-18 13:53:51
Programming
Elle Solomina, 2013-07-18 13:53:51

Is it possible to completely turn off part of the code of a template class method in C++?

Good day.
I have been developing in C++ for a long time, I don’t experience any special problems, but I ran into a problem that I don’t understand how to solve, although it should be easy to solve. Please tell me if there is any way to completely disable the generation of a certain section of code in the template function (as it would be when using the preprocessor). Example:

template <classT, bool alt = false>
Shablon
{

  void foo()
  {
      if (alt)
      {
         ((T*)this)->m_member.foo(); // ошибка компиляции если alt == false, и у класса T нет члена с методом foo.
         ((T*)this)->someFoo();
      }
      ((T*)this)->someFoo2();
  }

}

Answer the question

In order to leave comments, you need to log in

7 answer(s)
S
skor, 2013-07-22
@ElleSolomina

In C++, you can't just take it so that everything is fine, if without macros, of course. Simple solution with enable_if:

#include <boost/utility/enable_if.hpp>

template <class T, bool alt = false>
struct Shablon
{
  template <bool C>
  typename boost::enable_if_c<C>::type doSomething() {
    ((T*)this)->m_member.foo();
    ((T*)this)->someFoo();
  }

  template <bool C>
  typename boost::disable_if_c<C>::type doSomething() {
  }

  void foo()
  {
    doSomething<alt>();
    ((T*)this)->foo2();
  }
};

I
ixSci, 2013-07-18
@ixSci

You can use template specialization, in your case partial specialization, and write the necessary code there. Since you want (so simple) it will not work.
You can also make enable_if on foo a function within the same class.

A
abby, 2013-07-18
@abby

It seems to me that you need SFINAE, there are articles on the wiki and habré. I advise you to use type_traits. Visual c++ has __if_exists, but it's not cross-platform.

A
AxisPod, 2013-07-18
@AxisPod

Look aside en.cppreference.com/w/cpp/types
The main mechanisms are: template specializations and SFINAE.
Checking for the existence of a method can only be done with SFINAE (enable_if is based on SFINAE). In general, checking for the presence of a method by templates may often not be the best solution, because there may be inheritance.
But in general, take a look: stackoverflow.com/questions/87372/check-if-a-class-has-a-member-function-of-a-given-signature for a specific method existence check.

L
living, 2013-07-18
@living

In your case, you can just use a partial specialization of your class, or make an additional template class that runs the required methods, and specialize only it (something like this paste.kde.org/p29e289bb/ ). You can go further and use SFINAE to determine if the given methods are in class members, then you can hide your optional alt template parameter.

S
sdevalex, 2013-07-20
@sdevalex

template <int v>
struct Int2Type
{
        enum { value = v };
};

template <classT, bool alt = false>
class Shablon
{
  void doSomething(Int2Type<true>)
  {
     ((T*)this)->m_member.foo();
     ((T*)this)->someFoo();
  }

  void doSomething(Int2Type<false>)
  {
  }

  void foo()
  {
      DoSomething(alt);

      ((T*)this)->someFoo2();
  }
};

S
sdevalex, 2013-07-20
@sdevalex

__

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question