Extend templated struct with additional template parameter in C++ - c++

EXAMPLE
template< typename T >
struct A {
};
template< typename T, typename U >
struct A : A<T> {
};
int main() {
A<double> ad;
A<int, int> a;
}
COMPILE ERROR
g++ -std=c++17 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp:9:8: error: redeclared with 2 template parameters
struct A : A<T> {
^
main.cpp:4:8: note: previous declaration 'template<class T> struct A' used 1 template parameter
struct A {
^
main.cpp: In function 'int main()':
main.cpp:16:5: error: expected initializer before 'A'
A<int, int> aii;
^
Differrent template names works fine:
template< typename T >
struct A {
};
template< typename T, typename U >
struct AA : A<T> {
};
int main() {
AA<int, int> aa;
}
Wanna have same template name. It should be possible with variadic templates but I don't know how.
Thanks for your attention

You can use default parameters if you can define default:
template<typename T, typename U = /* default */>
struct A {
};
If you want to treat different amount of template parameter with different behavior, you can also use variadic templates and specialization for that:
template<typename...>
struct A;
template<typename T>
struct A<T> { // specialization for one parameter
};
template<typename T, typename U>
struct A<T, U> { // specialization for two parameter
};
int main() {
A<double> ad;
A<int, int> a;
// A<int, int, int> a; // error, undefined
}

Related

Issue with variadic template template parameter pack

Consider the following example:
template< class A, int B, class C>
struct Obj {
A a_obj;
static constexpr int b_value = B;
C c_obj;
};
template< template<class... > class ... Templates >
struct Foo;
template<>
struct Foo<Obj> {
Obj<void*, 5, float> obj;
};
int main() {
Foo<Obj> foo;
};
This is failing on me (g++ 7.2, with -std=c++17 with the following errors:
templ.cpp:16:15: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class ...> class ... Templates> struct Foo’
16 | struct Foo<Obj> {
| ^
templ.cpp:16:15: note: expected a template of type ‘template<class ...> class ... Templates’, got ‘template<class A, int B, class C> struct Obj’
templ.cpp: In function ‘int main()’:
templ.cpp:23:8: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class ...> class ... Templates> struct Foo’
23 | Foo<Obj> foo;
| ^
templ.cpp:23:8: note: expected a template of type ‘template<class ...> class ... Templates’, got ‘template<class A, int B, class C> struct Obj’
It seems that it is not getting matched, although it doesn't look like it thinks the template<class ...> class ... Templates is a syntax error, hence the parameter-parameter pack is parsing
Is this type of declaration (with some types, a constant value, then more types, or perhaps constant values at the end) unmatchable with variadic template template pack syntax? or perhaps I'm not using the syntax right
Edit
I modified the version to avoid non-types in declarations, but the problem persists:
template< template <class, class> class B, class A, class C>
struct Obj {
A a_obj;
//static constexpr int b_value = B;
B< C, A > b_obj;
C c_obj;
};
template< template<typename... > typename ... Templates >
struct Foo;
template<class A, class V>
struct Bar {
A a_obj;
//static constexpr int value = V;
V obj;
};
template<>
struct Foo<Obj> {
Obj<Bar, void*, float> obj;
};
int main() {
Foo<Obj> foo;
};
So it seems that is not that just non-types and types cannot be matches on a variadic pack, but any mixture of types and templates
I also tried adding an additional variadic pack:
template< template<typename... > typename ... Templates, class ... Types >
struct Foo;
But then I get:
error: parameter pack ‘template<class ...> class ... Templates’ must be at the end of the template parameter list
12 | template< template<typename... > typename ... Templates, class ... Types >
int B is a non-type template parameter. You have to declare Templates as template<class, auto, class> class if you want it to work with that particular class template, or redefine Obj to take a std::integral_constant to pass a value encapsulated in a type:
template<class A, class B, class C>
struct Obj {
A a_obj;
static constexpr int b_value = B::value;
C c_obj;
};
template<template<class...> class... Templates>
struct Foo;
template<>
struct Foo<Obj> {
Obj<void*, std::integral_constant<int, 5>, float> obj;
};
int main() {
Foo<Obj> foo;
}
Try it on godbolt.org.

How do I define function for part of typenames

I have a code like this
#include <iostream>
struct X {
};
template <typename T>
struct A {
static int a();
};
template <>
int A<int>::a() {
return 42;
}
template <>
int A<X>::a() {
return 0;
}
int main() {
std::cout << A<int>().a() << std::endl;
std::cout << A<X>().a() << std::endl;
return 0;
}
Now I want to return 42 for all types that are arithmetic i.e such that std::is_arithmetic<T>::type is std::true_type
I've tried
template <typename T, typename U = std::true_type>
struct A {
static int a();
};
template <typename T>
int A<T, typename std::is_arithmetic<T>::type>::a() {
return 42;
}
But I got following error:
a.cpp:12:51: error: invalid use of incomplete type ‘struct A<T, typename std::is_arithmetic<_Tp>::type>’
int A<T, typename std::is_arithmetic<T>::type>::a() {
^
a.cpp:7:8: error: declaration of ‘struct A<T, typename std::is_arithmetic<_Tp>::type>’
struct A {
^
and also tried
template <typename T>
struct A {
static int a();
};
template <typename T, typename E = typename std::enable_if<std::is_arithmetic<T>::value>::type>
int A<T>::a() {
return 42;
}
Error:
a.cpp:12:13: error: default argument for template parameter for class enclosing ‘static int A<T>::a()’
int A<T>::a() {
^
a.cpp:12:13: error: got 2 template parameters for ‘static int A<T>::a()’
a.cpp:12:13: error: but 1 required
What is correct way to achieve this? Does it exist at all?
I know I can do that, specializing all struct at once, but I don't want this because there are actually several functions more, that should be common
No, that's not going to work. Instead, try defining a separate type that will contain either 42 or 0, based on is_arithmetic. You can use boost::mpl::if_ for the purpose:
template< typename T > struct Res
{
typedef typename if_<
std::is_arithmetic<T>
, static_value<42>
, static_value<0>
>::type value;
};

GCC: template constructor instantiated when copy-constructor needed

In the following example, GCC >= 4.7 instantiates the template constructor (which you can observe by reading the error messages) although only the implicitly generated copy-constructor should be needed.
#include <type_traits>
// 'ambiguous' is ambiguous for 'ambiguous<int, int>'
template<typename A, typename B>
struct ambiguous : std::false_type {};
template<typename T>
struct ambiguous<int, T> : std::true_type {};
template<typename T>
struct ambiguous<T, int> : std::true_type {};
// quantity
template<typename Type>
class quantity
{
public:
quantity() = default;
// Copy-constructor is implicitly created
// Template constructor
template<
typename T,
typename = typename std::enable_if<ambiguous<Type, T>::value>::type
>
quantity(quantity<T>) {}
template<
typename T,
typename = typename std::enable_if<ambiguous<Type, T>::value>::type
>
void set(quantity<T>) {}
};
// main
int main()
{
quantity<int> a;
quantity<float> b;
b.set(a);
}
The above code compiles in GCC < 4.7, clang and MSVS (don't know which version, I used the one from http://rextester.com/runcode). In GCC >= 4.7 compilation fails with the following message:
main.cpp: In substitution of ‘template<class T, class> quantity<Type>::quantity(quantity<T>) [with T = int; <template-parameter-1-2> = <missing>]’:
main.cpp:39:12: required from here
main.cpp:23:9: error: ambiguous class template instantiation for ‘struct ambiguous<int, int>’
typename = typename std::enable_if<ambiguous<Type, T>::value>::type
^
main.cpp:9:8: error: candidates are: struct ambiguous<int, T>
struct ambiguous<int, T> : std::true_type {};
^
main.cpp:12:8: error: struct ambiguous<T, int>
struct ambiguous<T, int> : std::true_type {};
^
main.cpp: In function ‘int main()’:
main.cpp:31:10: error: initializing argument 1 of ‘void quantity<Type>::set(quantity<T>) [with T = int; <template-parameter-2-2> = void; Type = float]’
void set(quantity<T>) {}
So when invoking b.set(a);, GCC apparently looks for a copy constructor and on the way instantiates the template constructor which in turn instantiates ambiguous<int, int> which is (uhm...) ambiguous.
Question: Is GCC right to instantiate the template constructor even though a copy constructor is needed?
gcc is correct.
There are a couple of issues here which unfortunately have become conflated in your question:
First, the behavior of gcc < 4.7 is not fundamentally different; all versions of gcc since (at least) 4.4 reject the very similar program:
struct S;
template<typename, typename> struct U {};
template<typename T> struct U<S, T> {};
template<typename T> struct U<T, S> {};
struct S {
S() = default;
template<typename T, typename = typename U<S, T>::type> S(T) {}
};
int main() {
S a;
S b(a);
}
Note that the only real difference is that the copy-initialization is explicit rather than contained in a function call. Clang accepts this program, by the way.
Next, it's not fundamental to this issue that the copy constructor be involved (rule 12.8p6 in C++11); here's another similar program that gcc (all versions) rejects and clang accepts:
struct S {};
template<typename, typename> struct U {};
template<typename T> struct U<S, T> {};
template<typename T> struct U<T, S> {};
void f(S);
template<typename T> typename U<S, T>::type f(T);
int main() {
S a;
f(a);
}
The difference between clang and gcc is in the application of 14.8.2p8:
[...] [ Note: The evaluation of the substituted types and expressions can result in side effects such as the instantiation of class template specializations and/or function template specializations, the generation of implicitly-defined functions, etc. Such side effects are not in the "immediate context" and can result in the program being ill-formed. — end note ]
The ambiguity in the template specialization ambiguous<int, int> is outside the immediate context, so the program is ill-formed. (A supporting argument for this is that template specialization ambiguity does not appear in the succeeding list of reasons for type deduction to fail).
MSVC is different again; it accepts the following program that both clang and gcc reject:
template<typename T> struct U { typedef typename T::type type; };
struct S {
S() = default;
template<typename T, typename = typename U<T>::type> S(T) {}
};
int main() {
S a;
S b(a);
}
This is down to rule 12.8p6:
A declaration of a constructor for a class X is ill-formed if its first parameter is of type (optionally cv-qualified) X and either there are no other parameters or else all other parameters have default arguments. A member function template is never instantiated to produce such a constructor signature.
However, in order to determine whether a member function template instantiation is a constructor ill-formed with regard to 12.8p6, it is necessary to instantiate its declaration (cf. 14.7.1p9). Note that MSVC rejects the following program, so it isn't even consistent:
template<typename T> struct U { typedef typename T::type type; };
struct S {
S() = default;
template<typename T> S(T, typename U<T>::type *p = 0) {}
};
int main() {
S a;
S b(a);
}
This has some highly amusing behavior effects; MSVC accepts the following (ill-formed) program:
template<typename T> struct U { typedef typename T::type type; };
struct S {
S() = default;
template<typename T, typename = typename U<T>::type> S(T) {}
};
template<typename T> typename U<T>::type f(T) { return 0; }
int main() {
S a;
S b(a); // XXX
f(a);
}
However if the copy-initialization S b(a) is commented out, the program is rejected!

Curiously Recurring Templates?

I have a problem that Curiously Recurring Templates could help quite nicely, but I cant even get past a simple test.
template<typename T, int _size, typename OutterT>
class Foo {
};
template<typename T>
class Bar : public Foo<T, 2, Bar> {};
//typedef Bar<float> Vec2f;
int main()
{
return 0;
}
This results in the error
foo.cpp:7: error: type/value mismatch at argument 3 in template parameter list for ‘template<class T, int _size, class OuterT> class Foo’
foo.cpp:7: error: expected a type, got ‘Bar’
What am I missing.
compiled with g++ 4.2.1
template<typename T, int _size, typename OutterT>
class Foo {
};
template<typename T>
class Bar : public Foo<T, 2, Bar<T> > {};
// ^^^
Bar<float> x;
Since Bar is a template, you must provide the template argument to instantiate it into a class.

ambiguous template weirdness

I have the following code (sorry for the large code chunk, but I could not narrow it down any more)
template <bool B>
struct enable_if_c {
typedef void type;
};
template <>
struct enable_if_c<false> {};
template <class Cond>
struct enable_if : public enable_if_c<Cond::value> {};
template <typename X>
struct Base { enum { value = 1 }; };
template <typename X, typename Y=Base<X>, typename Z=void>
struct Foo;
template <typename X>
struct Foo<X, Base<X>, void> { enum { value = 0 }; };
template <typename X, typename Y>
struct Foo<X, Y, typename enable_if<Y>::type > { enum { value = 1 }; };
int main(int, char**) {
Foo<int> foo;
}
But it fails to compile with gcc (v4.3) with
foo.cc: In function ‘int main(int, char**)’:
foo.cc:33: error: ambiguous class template instantiation for ‘struct Foo<int, Base<int>, void>’
foo.cc:24: error: candidates are: struct Foo<X, Base<X>, void>
foo.cc:27: error: struct Foo<X, Y, typename enable_if<Y>::type>
foo.cc:33: error: aggregate ‘Foo<int, Base<int>, void> foo’ has incomplete type and cannot be defined
OK, so it's ambiguous. but I wasn't expecting it to be a problem as when using specialization it will almost always be some ambiguity. However this error is only triggered when using the class with enable_if<...>, if I replace it with a class like the following there is no problem.
template <typename X, typename Y>
struct Foo<X, Y, void > { enum { value = 2 }; };
Why does this class not cause an ambiguity while the others do? Isn't the two the same thing for classes with a true ::value?
Anyway, any hints as to what I am doing wrong are appreciated.
Thanks for the answers, my real problem (to get the compiler to select my first specialization) was solved by replacing struct Foo<X, Base<X>, void> with struct Foo<X, Base<X>, typename enable_if< Base<X> >::type > which seems to work the way I want.
The gist of your question is that you have:
template <typename X, typename Y, typename Z>
struct Foo {};
template <typename X>
struct Foo<X, Base<X>, void> {}; // #1
template <typename X, typename Y>
struct Foo<X, Y, typename whatever<Y>::type> {}; // #2
and you're trying to match it to
Foo<int, Base<int>, void>
Obviously, both specializations match (the first with X = int, the second with X = int, Y = Base<int>).
According to the standard, section 14.5.4, if there are more matching specializations, a partial ordering (as defined in 14.5.5.2) among them is constructed and the most specialized one is used. In your case, however, neither one is more specialized than the other. (Simply put, a template is more specialized than another, if you can replace each type parameter of the latter template with some type and in result get the signature of the former. Also, if you have whatever<Y>::type and you replace Y with Base<X> you get whatever<Base<X> >::type not void, i.e. there is not processing performed.)
If you replace #2 with
template <typename X, typename Y>
struct Foo<X, Y, void > {}; // #3
then the candidate set again contains both templates, however, #1 is more specialized then #3 and as such is selected.
aren't you missing a
<
symbol ?
I think you are missing a '<', a template should look like:
template< typename T >
struct myStruct
{};
//OR
template< class T >
struct myStruct
{};