Class template argument deduction (CTAD) with Concepts

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Can anyone answer me this question: Is CTAD supported on Concepts?

I have the following program shown at the bottom of this posting.

The error messages generated are shown after the program. It seems that reason the compilation fails is that there is no match for fixed_string constructor: fixed_string(fixed_string<...auto...>). I can find nothing about what "<...auto...>" might mean, so cannot create a suitable function. Is the reported lack of such a constructor the result of missing support for CTAD with Concepts, or have I missed something else?

---------------------------

The compiler used is the latest, stock gcc from Arch Linux:

~$ gcc --version
gcc (GCC) 10.1.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

-----------------------------

System used is stock Gnu/Linux from Arch:

~$ uname -a
Linux workbench 5.7.9-arch1-1 #1 SMP PREEMPT Thu, 16 Jul 2020 19:34:49 +0000 x86_64 GNU/Linux

-----------------------------

The command line generating this situation:

g++ -std=gnu++20 -O0 -g3 -pedantic -pedantic-errors -Wall -Wextra -Werror -c -pthread -fmessage-length=0 -fPIC -o test-concepts.o test-concepts.cpp

============================= Program ================================

#include <string>
#include <cstring>

template<std::size_t N>
struct fixed_string {
	static const constexpr std::size_t size__ = N;

	constexpr fixed_string(char const* s) :
		buf("") {
		for (std::size_t i = 0; i <= N; ++i)
			buf[i] = s[i];
	}
	constexpr operator char const*() const {
		return buf;
	}
	template<std::size_t M>
	constexpr bool compare(const fixed_string<M>& other) const {
		return N == M && ::strncmp(buf, other.buf, N) == 0;
	}

	char buf[N + 1];
};

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

template<fixed_string TARGET_NAME, typename TYPE>
concept NameMatcher = (TARGET_NAME.compare(TYPE::name__));

////////////////////////////////////////////

template<fixed_string NAME, typename TYPE>
class Member {
public:
	static const constexpr fixed_string name__ { NAME };

public:
	Member() :
		member_ { } {

	}

	template<fixed_string TARGET_NAME>
	const TYPE& get()
	    const requires NameMatcher<TARGET_NAME, TYPE> {
		return member_;
	}

protected:
	TYPE member_;
};

////////////////////////////////////////////

template<typename ... MEMBERS>
class Container: public MEMBERS... {
};

////////////////////////////////////////////

int main(int, char*[]) {
	Container<Member<"fred", int>, Member<"bert", float>,
		  Member<"alfie", bool>> container;
	return container.get<"fred">();
}

=============================== Messages ===============================

test-concepts.cpp:47:35: error: class template argument deduction failed:
   47 |  const TYPE& get() const requires NameMatches<TARGET_NAME, TYPE> {
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-concepts.cpp:47:35: error: no matching function for call to ‘fixed_string(fixed_string<...auto...>)’ test-concepts.cpp:8:12: note: candidate: ‘template<long unsigned int N> fixed_string(const char*)-> fixed_string<N>’
    8 |  constexpr fixed_string(char const* s) :
      |            ^~~~~~~~~~~~
test-concepts.cpp:8:12: note: template argument deduction/substitution failed:
test-concepts.cpp:47:35: note:   couldn’t deduce template parameter ‘N’
   47 |  const TYPE& get() const requires NameMatches<TARGET_NAME, TYPE> {
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-concepts.cpp:5:8: note: candidate: ‘template<long unsigned int N> fixed_string(fixed_string<N>)-> fixed_string<N>’
    5 | struct fixed_string {
      |        ^~~~~~~~~~~~
test-concepts.cpp:5:8: note: template argument deduction/substitution failed: test-concepts.cpp:47:35: note: mismatched types ‘fixed_string<N>’ and ‘fixed_string<...auto...>’
   47 |  const TYPE& get() const requires NameMatches<TARGET_NAME, TYPE> {
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-concepts.cpp:28:1: note: candidate: ‘template<long unsigned int N> fixed_string(const char (&)[N])-> fixed_string<(N - 1)>’
   28 | fixed_string(char const (&)[N]) -> fixed_string<N - 1>;
      | ^~~~~~~~~~~~
test-concepts.cpp:28:1: note: template argument deduction/substitution failed: test-concepts.cpp:47:35: note: mismatched types ‘const char [N]’ and ‘fixed_string<...auto...>’
   47 |  const TYPE& get() const requires NameMatches<TARGET_NAME, TYPE> {
      |                                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test-concepts.cpp: In function ‘int main(int, char**)’:
test-concepts.cpp:65:19: error: request for member ‘get’ is ambiguous
   65 |  return container.get<"fred">();
      |                   ^~~
test-concepts.cpp:47:14: note: candidates are: ‘template<fixed_string<...auto...> TARGET_NAME> const TYPE& Member<NAME, TYPE>::get() const requires <erroneous-expression> [with fixed_string<...auto...> TARGET_NAME = TARGET_NAME; fixed_string<...auto...> NAME = fixed_string<5>{"alfie"}; TYPE = bool]’
   47 |  const TYPE& get() const requires NameMatches<TARGET_NAME, TYPE> {
      |              ^~~
test-concepts.cpp:47:14: note: ‘template<fixed_string<...auto...> TARGET_NAME> const TYPE& Member<NAME, TYPE>::get() const requires <erroneous-expression> [with fixed_string<...auto...> TARGET_NAME = TARGET_NAME; fixed_string<...auto...> NAME = fixed_string<4>{"bert"}; TYPE = float]’ test-concepts.cpp:47:14: note: ‘template<fixed_string<...auto...> TARGET_NAME> const TYPE& Member<NAME, TYPE>::get() const requires <erroneous-expression> [with fixed_string<...auto...> TARGET_NAME = TARGET_NAME; fixed_string<...auto...> NAME = fixed_string<4>{"fred"}; TYPE = int]’
test-concepts.cpp:65:31: error: expected primary-expression before ‘)’ token
   65 |  return container.get<"fred">();
      |                               ^

================ Preprocessor output ===========================

It's nearly 40,000 lines - too much to post here



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux