-4

Does C++ have a standard way which do the same things as std::__and_ and std::__or_ do? I always using them in my private project, but now I'm writing a public project, and these template helper seems not part of the C++ STL API, so is there a standard way? The worst case I can see is to copy the std::__and_ from the header to my project, but I think this is not very elegant.

An example:

// if a type is a pointer or a reference, do some thing.
template<typename T>
std::enable_if_t<std::__or_<std::is_pointer<T>, std::is_reference<T>>::type>
DoSomething(T t) {
  // do something
}
underscore_d
  • 6,309
  • 3
  • 38
  • 64
  • 3
    Please include an example of your current code that needs rewritten, so people know what you're trying to do. It does need rewritten, as you should not rely on implementation details like those or anything else beginning with a double underscore or a single one followed by an uppercase letter. It might be that you can simple use the normal logical operators in the expressions, but who could say without any info on how you are currently doing it otherwise? – underscore_d Aug 31 '20 at 11:58
  • 2
    This looks like a standard library implementation details. Not all std implementations may have it. Don't assume just naming these things is enough to describe what they do, especially when asking about replacements for them. – StoryTeller - Unslander Monica Aug 31 '20 at 11:59
  • 2
    Your question lacks clarity on what exactly is the functionality you want to have. in C++ standard "or" and "and" operations : && (expression AND), || (expression OR), and bitwise: & (logical AND)., (logical OR)|, ^ (logic XOR) operators? – snus74 Aug 31 '20 at 12:02
  • 3
    Why not just use && and || ...? `std::enable_if_t::value || std::is_reference::value>>` – user253751 Aug 31 '20 at 13:18
  • 1
    The question was not closed because people didn't know how to use `enable_if`; it was closed because you were leaving them to guess how you were using `__and_`/`__or_`. It has since been reopened because that is no longer a problem. The replacement is simply to use the normal logical operators. Why you ever felt the need to use `__and_`/`__or_` instead of those is not clear still, however. – underscore_d Aug 31 '20 at 13:52
  • 1
    @user253751 consider this:`enable_if<__and_<_Cond...>::value>::type;` in this case you can't use "||" as easy as that. –  Aug 31 '20 at 13:58
  • So then you should have specified in the question that you needed to support arbitrary numbers of template arguments. – underscore_d Aug 31 '20 at 14:02
  • @underscore_d, sorry I'm mad because some unfriendly participants, I didn't come up with an example because there are lots or thing `std::__and_` can do, even it can using with combine types for inheritance, until now I just can't come up a fully example, and the purpose of this question is simple, is there a portable way to using these convenient template helpers. –  Aug 31 '20 at 14:06
  • As far as I can tell, there is, and rajdakin has posted it: `std::conjunction`/`std::disjunction` perform short-circuit logical AND/OR resp., over an arbitrary number of type traits. That expects trait structs having boolean `::value` members. Do you also need to include booleans from other sources? – underscore_d Aug 31 '20 at 14:11
  • @underscore_d that's the "have to copy source code" part I talked about, I just ask to know if there's public API for easy use, as simple as that, because I'm not sure how large work it could be to simply use this with MSVC –  Aug 31 '20 at 14:17
  • What versions of MSVC and C++ do you have to use? They were added in C++17, so I would imagine it has caught up with them by now and supports them when in C++17 mode. Constraints on usable versions of C++ and compiler are also things that should be in the Q. If you omit specific requirements from the Q, you get answers that don't meet them. – underscore_d Aug 31 '20 at 14:21
  • 1
    I just checked, the MSVC out project using has std::conjunction, porting things may be not that hard as I imagine. –  Aug 31 '20 at 14:27
  • 1
    @ravenisadesk Well in that case you want to use && instead of ||. And yes, you can use a parameter pack with &&. I don't remember the syntax OTOH – user253751 Aug 31 '20 at 14:51
  • @user253751 Right, C++17 also adds fold expressions, so something like `std::enable_if_t< (... && Ts::value), whatever >` could work. [cppreference](https://en.cppreference.com/w/cpp/types/conjunction) notes that `std::(con|disjunction)` are unlike fold expressions in that the trait classes perform short-circuiting of instantiations whereas fold expressions don't; see [this](https://stackoverflow.com/questions/55161235/is-there-a-reason-to-use-stdconjunction-stddisjunction-instead-of-a-fold-exp) for more info. – underscore_d Sep 01 '20 at 09:26

1 Answers1

1

If you are meaning, by that, a structure/constexpr boolean that ands the template parameters, then it would seems like std::conjunction and std::disjunction are the C++17 structs you want (note that the values are accessible directly with the _vs ones).

They are in the type_traits header.

rajdakin
  • 74
  • 6