2

I'm attempting to learn requires expressions as a stepping stone towards getting comfortable with the concepts mechanic. The easiest way I could come up with was to ease into it by replacing all my static_assert expressions in any templates with requires expressions.

One that is a little tricky to understand is the difference between std::is_base_of_v<> and std::derived_from<>.

Are the following requires and static_assert expressions equivalent, i.e., do the same thing? If not, what will? My intent was to require, after any possible reference/cv/pointer decoration was removed, that GameType and GameBase were derived from each other:

class GameBase {
//...
};

//...

#include <concepts>
#include <string>
#include <type_traits>

template<typename GameType>
requires(std::derived_from<GameType, GameBase>)
class Engine {
public:
    static_assert(std::is_base_of_v<std::remove_cvref_t<std::remove_pointer_t<GameBase>>, std::remove_cvref_t<std::remove_pointer_t<GameType>>>, "GameType template parameter is not derived from GameBase.");
    static void Initialize(const std::string& title, const std::string& cmdString) noexcept;
    static void Run() noexcept;
    static void Shutdown() noexcept;
    static const bool Available() noexcept;

private:
    static inline bool m_initCalled{false};
    static inline bool m_shutdownCalled{false};
};
Casey
  • 10,297
  • 11
  • 59
  • 88
  • 2
    std::derived_from == true only for public inheritance or exact same class. std::is_base_of == true also for private inheritance. https://en.cppreference.com/w/cpp/concepts/derived_from – kiner_shah Mar 23 '23 at 07:11
  • Maybe duplicate of https://stackoverflow.com/questions/23095310/c-concepts-vs-static-assert – 康桓瑋 Mar 23 '23 at 07:38
  • @康桓瑋 I'm confused how that answer predates c++20 by 6 years. Anyhow, I think it's a great dupe except there's a few tidbits that isn't quite accurate (unsurprisingly). – Passer By Mar 23 '23 at 07:58

1 Answers1

-2

Here is an example that answer your question.

#include <type_traits>
#include <concepts>

template <typename T>
struct foo;

template <typename T>
requires(std::same_as<T, int>)
struct foo<T>{};

template <typename T>
requires(std::same_as<T, double>)
struct foo<T>{};

template <typename T>
struct bar;

/// error, static_assert cannot be used as specilization
/*
template <typename T>
struct bar<T>{
    static_assert(std::is_same_v<int, T>);
};

template <typename T>
struct bar<T>{
    static_assert(std::is_same_v<double, T>);
};
*/
macomphy
  • 413
  • 1
  • 9
  • 2
    How does this answer the question? Essentially posting code by itself as an answer isn't really helpful. – Casey Mar 23 '23 at 09:05
  • It is a situation that you can use requires (foo) but cannot use static_assert (bar). @Casey – macomphy Mar 23 '23 at 09:26
  • Explanations belong in the answer. As stated, the answer is basically code-only, which typically is not helpful. Use words to explain, and use the example to augment (not replace) that explanation. – JaMiT Mar 24 '23 at 06:17
  • the comment "/// error, static_assert cannot be used as specilization" is the all explanation your need, what else do you want? @JaMiT – macomphy Mar 24 '23 at 06:59
  • @macomphy I suppose one way to describe what I want is something that someone other than yourself would find easy to understand. You can argue until you're blue in the face that you've done all that is required of you, but that won't help others figure out what you're trying to communicate. – JaMiT Mar 25 '23 at 02:26
  • I have only one question. Have you tried to compile this example on some place before you say it's not clear? @JaMiT – macomphy Mar 27 '23 at 01:58
  • @macomphy *"Have you tried to compile this example on some place before you say it's not clear?"* -- No. And I see no reason to. In fact, I would recommend **against** compiling code found on the internet before the purpose of the code is clear. – JaMiT Mar 28 '23 at 06:31
  • Don't you even know of an online compiler site? @JaMiT – macomphy Mar 28 '23 at 06:50
  • @macomphy You said only one question. I entertained that one, despite its questionable relevance. Have fun arguing until you are blue in the face. – JaMiT Mar 28 '23 at 07:01