Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass a reference type to a template to specify following non-type template parameters' types?

Consider an example:

#include <type_traits>

template <class T, T>
struct has_duplicates_info { };

template <class T, T...>
struct has_duplicates;

template <class T, T First, T... Others>
struct has_duplicates<T, First, Others...>:
          has_duplicates<T, Others...>,
          has_duplicates_info<T, First> {
   static constexpr bool value =
      std::is_base_of<has_duplicates_info<T, First>, has_duplicates<T, Others...>>::value
        || has_duplicates<T, Others...>::value;
};

template <class T, T Last>
struct has_duplicates<T, Last>: has_duplicates_info<T, Last>, std::false_type { };

int a, b;

int main() {
   static_assert(!has_duplicates<int, 0, 1, 2>::value, "has_duplicates<int, 0, 1, 2>::value");
   static_assert(has_duplicates<int, 1, 2, 2, 3>::value, "!has_duplicates<int, 1, 2, 2, 3>::value");
   static_assert(has_duplicates<int&, a, a, b>::value, "!has_duplicates<int&, a, a, b>::value");
}

This compiles fine with clang but not with gcc. The problem is in a line:

static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value");

where compiler suggests that has_duplicates<int&, a, a, b> is an incomplete type:

has_duplicates.cc:26:18: error: incomplete type ‘has_duplicates<int&, a, a, b>’ used in nested name specifier
    static_assert(has_duplicates<int&, a, a, b>::value, "has_duplicates<int&, a, a, b>::value");
                  ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

So... which compiler is right?

Edit:

To clarify I am not trying to check if the runtime values behind variables passed to has_duplicates contains duplicates only if there are duplicated references passed to this trait... E.g. the code below compiles successfully in both gcc and clang:

template <int &X>
struct a { };

int b;

int main() {
   a<b> c;
}
like image 712
W.F. Avatar asked Aug 31 '16 15:08

W.F.


People also ask

What can be passed by non-type template parameters during?

What can be passed by non-type template parameters during compile time? Explanation: Non-type template parameters provide the ability to pass a constant expression at compile time. The constant expression may also be an address of a function, object or static class member.

Can we use non-type parameters as arguments template?

Non-type template arguments are normally used to initialize a class or to specify the sizes of class members. For non-type integral arguments, the instance argument matches the corresponding template parameter as long as the instance argument has a value and sign appropriate to the parameter type.

Can a template be a template parameter?

A template argument for a template template parameter is the name of a class template. When the compiler tries to find a template to match the template template argument, it only considers primary class templates. (A primary template is the template that is being specialized.)

How will you restrict the template for a specific datatype?

There are ways to restrict the types you can use inside a template you write by using specific typedefs inside your template. This will ensure that the compilation of the template specialisation for a type that does not include that particular typedef will fail, so you can selectively support/not support certain types.


1 Answers

First off, it's definitely a bug in gcc. You're nearly correct in your diagnosis of the nature of the bug, but it's not that gcc doesn't accept reference-type non-type template parameters, it's rather that gcc fails to recognise reference-type non-type template parameters as a class template partial specialization where the reference type is a previous template parameter:

template<int, class T, T> struct S;  // #1
template<class T, T A> struct S<0, T, A> {};  // #2
int i;
S<0, int&, i> s;  // #3 error: aggregate 'S<0, int&, i> s' has incomplete type

#2 is a perfectly legitimate partial specialization of #1 and should be matched by the instantiation #3, per [temp.class.spec.match] and [temp.deduct].

Fortunately, there's a simple workaround, which is to provide a further partial specialization for reference types:

template<class R, R& A> struct S<0, R&, A> {};

A correct compiler like clang will also be fine with this.

In your case the further partial specializations would be:

template <class R, R& First, R&... Others>
struct has_duplicates<R&, First, Others...> // ...
template <class R, R& Last>
struct has_duplicates<R&, Last> // ...
like image 92
ecatmur Avatar answered Oct 14 '22 10:10

ecatmur