What are the rules for deducting a parameter packet

Can someone tell me why this is not working?

template<char... cs> struct StaticString {};

template<char... tail, char... prefix>
constexpr bool startsWith(StaticString<prefix..., tail...>, StaticString<prefix...>)
{
    return true;
}

static_assert(startsWith(StaticString<'a', 'b'>(), StaticString<'a'>()),
              "ab starts with a");

Why is the tail lead empty?

+4
source share
2 answers

From cpprefrence - Parameter Package

Explanation
...
In the template of the primary class, the package of template parameters must be the last parameter in the list of template parameters.
In the function template, the package of template parameters can be displayed earlier in the list provided that the following arguments of the function can be used to derive all the following parameters or have default arguments

and cppreference - output of template argument


...
P , <T> <I>, Pi Ai A. Pi , A.

, , .


, . startsWith . , StaticString left

template<char... tail>
constexpr bool startsWith(StaticString<tail...>, StaticString<>)
{
    return true;
}

startsWith, StaticString

template<char... tail1, char... tail2>
constexpr bool startsWith(StaticString<tail1...>, StaticString<tail2...>)
{
    return false;
}

, , , ,

template<char prefix, char... tail1, char... tail2>
constexpr bool startsWith(StaticString<prefix, tail1...>, StaticString<prefix, tail2...>)
{
    return startsWith(StaticString<tail1...>(), StaticString<tail2...>());
}

static_assert ,

static_assert(startsWith(StaticString<'a', 'b'>(), StaticString<'a'>()),
              "ab starts with a");
static_assert(startsWith(StaticString<'a', 'b'>(), StaticString<'a', 'b'>()),
              "ab starts with ab");
static_assert(startsWith(StaticString<'a', 'b'>(), StaticString<'a', 'c'>()),
              "ab does not start with ac");
static_assert(startsWith(StaticString<'a', 'b'>(), StaticString<'x', 'a'>()),
              "ab does not start with xa");

(Ubuntu 16.04, g++ 5.4)

a.cpp: 23: 1: : : ab ac
 static_assert (startsWith (StaticString < 'a', 'b' > (), StaticString < 'a', 'c' > ()), "ab ac"
 ^
a.cpp: 24: 1: : : ab xa
 static_assert (startsWith (StaticString < 'a', 'b' > (), StaticString < 'x', 'a' > ()), "ab xa"
 ^

+4

StaticString<prefix..., tail...> , , prefix tail .

.

+3

Source: https://habr.com/ru/post/1693244/


All Articles