public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Darrell.Wright at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/97134] New: partial specialization not more specialized when using CNTTP
Date: Mon, 21 Sep 2020 05:52:40 +0000	[thread overview]
Message-ID: <bug-97134-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97134

            Bug ID: 97134
           Summary: partial specialization not more specialized when using
                    CNTTP
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Darrell.Wright at gmail dot com
  Target Milestone: ---

g++, tried trunk on CE along with 10.2 and 10.1 will say the following code is
not more specialized when JSONNAMETYPE is a CNTTP(json_name).  When it's a
NTTP(char const *) it is ok with it.  The code works in MSVC's C++20 mode.
https://godbolt.org/z/7v4nx5

#include <cstddef>
#include <iostream>
#include <string_view>
#include <utility>

template <std::size_t N>
struct json_name {
  static_assert(N > 0);
  char const m_data[N]{};

 private:
  template <std::size_t... Is>
  constexpr json_name(char const (&ptr)[N], std::index_sequence<Is...>)
      : m_data{ptr[Is]...} {}

 public:
  constexpr json_name(char const (&ptr)[N])
      : json_name(ptr, std::make_index_sequence<N>{}) {}

  // Needed for copy_to_iterator
  [[nodiscard]] constexpr char const *begin() const { return m_data; }

  // Needed for copy_to_iterator
  [[nodiscard]] constexpr char const *end() const {
    return m_data + static_cast<ptrdiff_t>(size());
  }

  [[nodiscard]] static constexpr std::size_t size() noexcept { return N - 1; }

  template <std::size_t M>
  constexpr bool operator==(json_name<M> const &rhs) const {
    if (N != M) {
      return false;
    }
    for (std::size_t n = 0; n < N; ++n) {
      if (m_data[n] != rhs.m_data[n]) {
        return false;
      }
    }
    return true;
  }

  constexpr operator std::string_view() const {
    return std::string_view(m_data, size());
  }
};
template <typename... Chars>
json_name(Chars...) -> json_name<sizeof...(Chars)>;

template <std::size_t N>
json_name(char const (&)[N]) -> json_name<N>;

#ifdef USE_CNTTP
#define JSONNAMETYPE json_name
#else
#define JSONNAMETYPE char const *
#endif

template <JSONNAMETYPE Name, typename U>
struct X : std::false_type {
  static constexpr std::string_view name = Name;
};

template <JSONNAMETYPE Name>
struct X<Name, bool> : std::true_type {
  static constexpr std::string_view name = Name;
};

int main() {
  static constexpr char const A[] = "A";  
  static constexpr char const B[] = "B";  
  auto a = X<A, int>::name;
  auto b = X<B, bool>::name;
  std::cout << a << b << '\n';
}

             reply	other threads:[~2020-09-21  5:52 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-21  5:52 Darrell.Wright at gmail dot com [this message]
2020-09-23 13:01 ` [Bug c++/97134] " ppalka at gcc dot gnu.org
2021-03-20 14:57 ` omer.rosler at gmail dot com
2021-04-09 21:20 ` ppalka at gcc dot gnu.org
2021-04-13  3:23 ` cvs-commit at gcc dot gnu.org
2021-04-13  3:23 ` ppalka at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-97134-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).