Is it possible to completely disable parts of the code method of the template class in C++?

Good time of day.


Develop in C++ for a long time, the special problems do not feel, but now faced with the task that don't understand how to solve, although it can be solved easily. Please tell me is there any way to completely disable the generation of a specific piece of code in a template function (as it would when using the preprocessor). Example:

template <classt, bool alt="false">
Shablon
{

 void foo()
{
 if (alt)
{
 ((T*)this)->m_member.foo(); // compile error if alt == false and the class T has no member method foo.
((T*)this)->someFoo();
}
((T*)this)->someFoo2();
}

}</classt,>
October 3rd 19 at 02:30
7 answers
October 3rd 19 at 02:32
Solution
In C++ you can't just take everything to be well, if without macros, of course. Easy solution with enable_if:

#include <boost enable_if utility.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();
}
};

</alt></c></bool></c></bool></class></boost>
Thank you, perhaps it is this code most closely allows you to realize the essence, i.e. the dependence on compile-time constants. Sorry to write a lot. Since we are here together to find out what is easy in C++ will not work without operators branching compile-time, perhaps I should note that is your option as a solution. - jay commented on October 3rd 19 at 02:35
October 3rd 19 at 02:34
You can use a specialization of the template, in your case partial(partial specialization) to write the necessary code. As you want(so simple) will not work.
You can still make the enable_if on the foo function inside the same class.
As you want(so simple) will not work.

Thank you, sorry for that. want to directly declare the set of features in the template parameter, without changing the class interface. But probably will have in a roundabout way :)

p.s: it is strange that this is impossible, because the template parameter is known at compile time and the compiler might not raking to throw this code... but maybe it's backwards compatible with any goto. In General, expect the D to such a convenience :) - jay commented on October 3rd 19 at 02:37
It is therefore advocated if the compile step instead of all these ugly designs on SFINAE. But when it has to be... - jay commented on October 3rd 19 at 02:40
Yeah, if, and switch statements compile-time overdue. - Ashlynn commented on October 3rd 19 at 02:43
October 3rd 19 at 02:36
I think you need SFINAE, wiki and habré there are article. Suggest to use type_traits. Visual c++ is __if_exists, but it crossplatforming.
Thank you, in this case, you had better just turned __if_exists. This template is a piece of GUI with Studio project, and there is quite a lot of WinApi. - jay commented on October 3rd 19 at 02:39
October 3rd 19 at 02:38
Look to the side en.cppreference.com/w/cpp/types

Of the major mechanisms for this: specialization of templates and SFINAE.

Check method can only be done using the SFINAE (enable_if SFINAE is based on). Generally check the availability of the method patterns often may not be the best solution because it can be inheritance.

But do take a look: stackoverflow.com/questions/87372/check-if-a-class-has-a-member-function-of-a-given-signature specific check method.
Thank you for the advice abby zayuzat __if_exists. In the project it accurately enough for the eyes, anyway, what I wanted C++ to do this, unfortunately, will not work. - jay commented on October 3rd 19 at 02:41
October 3rd 19 at 02:40
In your case you can just use partial specialization of your class, or to make an additional template class that triggers the required methods, and specialise only it ( something like this paste.kde.org/p29e289bb/ ). You can go ahead and use SFINAE to determine whether these methods are in the class members, then it will be possible to hide your additional template parameter alt.
October 3rd 19 at 02:42
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();
}
};</false></true></classt,></int>
In foo() a typo of course...
doSomething(alt);


Similar code is contained in the book "Andrei Alexandrescu — Modern C++design" - jay commented on October 3rd 19 at 02:45
Thank you, it is clear that it is possible :) But it is heaps of unnecessary code. Just imagine, there in the code about two dozen different pieces that need to be turned on and off depending on the situation. And, accordingly, for what would it work, you have to write about 40 more functions. :) - jay commented on October 3rd 19 at 02:48
g++ 4.3.4 error:
main.cpp:43: error: no matching function for call to 'Shablon<t1, false>::doSomething(bool)'
main.cpp:29: note: candidates are: void Shablon<t, option>::doSomething(Int2Type<1>) [with T = T1, bool alt = false]
main.cpp:34: note: void Shablon<t, option>::doSomething(Int2Type<0>) [with T = T1, bool alt = false]
</t></t></t1>

In your case you want to call, probably, right?
doSomething(Int2Type<alt>());
</alt>
- Ashlynn commented on October 3rd 19 at 02:51
October 3rd 19 at 02:44
__

Find more questions by tags ProgrammingC++