I want to use perf. forwarding with initializer_list (curly braces), but I've failed in writing code that could be compiled.
How to make type deduction working in the following sample of code?
#include <utility>
template <class _T> struct B {
_T a;
_T b; };
template <class _T> void bar(B<_T>&& a) {}
template <class _T> void bar(B<_T>& a) {}
template <class _T> struct A {
template <class __T>
void foo(__T&& a) {
bar(std::forward<__T>(a));
} };
int main() {
A<int> a;
a.foo({1, 3}); }
I know that it's possible to do perfect forwarding with variadic template argument, like this:
#include <utility>
template <class _T>
struct B {
_T a;
_T b;
};
template <class _T>
void bar(_T&& v1, _T&& v2) {
B<_T> b{v1, v2};
}
template <class _T>
void bar(_T& v1, _T& v2) {
B<_T> b{v1, v2};
}
template <class _T>
struct A {
template <class... Args>
void foo(Args&&... args) {
bar(std::forward<Args>(args)...);
}
};
int main() {
A<int> a;
a.foo(1, 3);
}
But I want to call foo with cutee curly braces.
{1, 3} has no type, so cannot be deduced for "generic" template type.
You might use overload with std::initializer_list to handle it;
template <class T>
struct A {
template <class U>
void foo(U&& a) {
bar(std::forward<U>(a));
}
template <class U>
void foo(std::initializer_list<U> a) {
bar(a); // assuming bar(std::initializer_list<U>)
}
};
int main() {
A<int> a;
a.foo({1, 3});
}
You can't. Along with other serious shortcomings, std::initializer_list cannot be deduced by simply using the {...} syntax.
a.foo(std::initializer_list{1, 3})
Will pass deduction properly, but you won't be able to invoke bar with it as you expect an instance of B.
live example on wandbox.org
How about just
a.foo(B<int>{1, 3})
?
Related
I will explain my question based on following example:
template <typename Param1, typename Param2>
class foo1
{
void lol();
};
template <typename Param1, typename Param2>
class foo2
{
void lol();
};
////////////////////////// FIRST OPTION //////////////////////////
template <typename Param1, typename Param2>
void func(foo1<Param1, Param2> a)
{
a.lol();
a.lol();
}
template <typename Param1, typename Param2>
void func(foo2<Param1, Param2> a)
{
a.lol();
}
////////////////////////// SECOND OPTION //////////////////////////
template <typename Foo_>
void func(Foo_ a)
{
a.lol();
a.lol();
}
///////////////////////////////////////////////////////////////////
int main()
{
return 0;
}
My goal is to write two overloads of func - for foo1 and foo2.
The FIRST OPTION works fine, but I don't like that I have to write template parameters for func that are not used in it.
Is there some way to avoid writing template parameters in signature of func?
I thought of writing something like this SECOND OPTION, but the problem is that overloads do different things.
The template parameters are used. Without them, foo1 and foo2 are just class templates and not a classes.
A simple way to minimize the typing would be to use template parameter packs:
template<class... T>
void func(foo1<T...> a) {
a.lol();
a.lol();
}
template<class... T>
void func(foo2<T...> a) {
a.lol();
}
If you need this check a lot, you can create concepts (since C++20):
#include <type_traits>
// concept for foo1:
template <class T> struct is_foo1 : std::false_type {};
template <class P1, class P2> struct is_foo1<foo1<P1, P2>> : std::true_type {};
template <typename T> concept Foo1Type = is_foo1<T>::value;
// concept for foo2:
template <class T> struct is_foo2 : std::false_type {};
template <class P1, class P2> struct is_foo2<foo2<P1, P2>> : std::true_type {};
template <typename T> concept Foo2Type = is_foo2<T>::value;
Then the func overloads become simpler:
void func(Foo1Type auto a) {
a.lol();
a.lol();
}
void func(Foo2Type auto a) {
a.lol();
}
Demo
I want to specialize a member function of a class template as follows:
#include <concepts>
template <typename T>
struct S {
void f();
};
template <typename T>
void S<T>::f() {
}
// (0) This is fine.
template <>
void S<int>::f() {
}
// (1) This triggers an error.
template <std::integral T>
void S<T>::f() {
}
The specialization (0) is fine, but specializes f() only for the int type. Instead, I would like to specialize it, e.g., for any integral type, as in (1). Is this possible using C++20 concepts? Notice that std::integral is just an example and that my specific case makes use of user-defined concepts.
Simply use a trailing requires-clause. The compiler will choose the most constrained function:
#include <concepts>
#include <iostream>
template <typename T>
struct S {
void f();
void f() requires std::integral<T>;
};
template <typename T>
void S<T>::f() {
std::cout << "general\n";
}
template <typename T>
void S<T>::f() requires std::integral<T> {
std::cout << "constrained\n";
};
int main() {
S<int> x;
S<double> y;
x.f(); // prints constrained
y.f(); // prints general
return 0;
}
Why compiler can deduce T with this code:
#include <vector>
template<typename T>
void foo(T& t) {}
int main(void) {
std::vector<uint8_t> vec = { 1,2,3 };
foo(vec);
return 0;
}
But fails with this code:
#include <vector>
#include <type_traits>
template<typename T>
void foo(typename std::enable_if<true, T>::type& t) {}
int main(void) {
std::vector<uint8_t> vec = { 1,2,3 };
foo(vec);
return 0;
}
I want to use second construct, to select between two template functions, based on passed class method existence.
In the second case you have a non-deduced context, in other words, the compiler cannot deduce the type.
The simplest example of a non-deduced context is
template<typename T>
struct Id
{
using type = T;
};
template<typename T>
void foo(typename Id<T>::type arg); // T cannot be deduced
As explained by vsoftco, you have a non-deduced context.
For SFINAE, you may use one of the following:
template<typename T>
std::enable_if_t<condition_dependent_of_T, ReturnType>
foo(T& t) {}
or
template<typename T, std::enable_if_t<condition_dependent_of_T>* = nullptr>
ReturnType foo(T& t) {}
To visualize the problem let's analyse an example:
template <class>
struct foo {
using type = float;
};
template <>
struct foo<bool> {
using type = int;
};
template <>
struct foo<int> {
using type = int;
};
template <class T>
void bar(foo<T>::type t) { }
int main() {
bar(int{});
}
Now in the line bar(int{}); both types bool as well as int matches to a template parameter T. Which one value should be deduced then? This is only the one example why non-deduced context is strictly necessary!
By using template template parameters one can pass to a class a templated class without specifying types on its parameters. I was wondering is there a way to pass into a template template parameter a templated signature of a function to be able to specialize which variant of the function is to be considered forward.
To be clear - I know I cannot do that:
template <class T>
void foo() { /*...*/ }
template <template <class...> class FooType>
struct Foo { /*...*/ };
int main() {
Foo<decltype(foo)> f;
}
But somehow I would like to be able to pass templated signature of function to Foo. Is it even possible?
I couldn't believe that this is not possible so I searched a bit and found a way to do exactly what I wanted. I used templated using with a syntax:
template <template<class... Args> class FooType>
struct Foo {
FooType<int> ft;
};
template <class Res, class... Args>
using FooSignature = Res(*)(Args...);
int foo() {
return 1;
}
int main() {
Foo<FooSignature> f;
f.ft = foo;
}
This however still leaves the question how can this be possible since the standard states something opposite.
In the example below one has a template template parameter that accepts the preferred signature for the function.
Because of the specialization and the lack of a body for the template class, only types for callables are accepted.
It is a generalization of what the OP actually asked:
#include<cassert>
template<typename F>
struct S;
template<typename R, typename... Args>
struct S<R(Args...)> {
using type = R(*)(Args...);
};
template<template<typename> class F>
struct T {
typename F<void(int)>::type ft;
typename F<double(double, double)>::type gt;
};
void f(int) { }
double g(double x, double y) { return x+y; }
int main() {
T<S> t;
t.ft = f;
t.gt = g;
t.ft(42);
auto v = t.gt(1., 1.);
assert(v == 2.);
}
As can be seen in this answer
Template of function pointer is illegal in C++
The C++ Standard says in $14/1,
A template defines a family of classes or functions.
Further quoting from the linked answer:
Please note that it does NOT say "A template defines a family of classes, functions or function pointers"
However, you can pass concrete function pointers, and specialise on their signature:
#include <iostream>
template <class T>
void foo(T) { }
template <typename>
struct Foo;
template<typename T>
struct Foo<void(T)>
{
void cb() { std::cout << "T\n"; }
};
template<>
struct Foo<void(int)>
{
void cb() { std::cout << "int\n"; }
};
template<>
struct Foo<void(double)>
{
void cb() { std::cout << "double\n"; }
};
int main()
{
Foo<decltype(foo<int >)>().cb(); // outputs 'int'
Foo<decltype(foo<double>)>().cb(); // outputs 'double'
Foo<decltype(foo<char >)>().cb(); // outputs 'T'
return 0;
}
template of template is still a template.
template <class T>
void foo() { /*...*/ }
template <typename T>
struct Foo { /*...*/ };
int main() {
Foo<decltype(foo<int>)> f;
}
You cannot pass a function template as an argument. What you can do is wrap a function template in a generate lambda taking a tag parameter:
template <class T> struct tag_t { using type = T; };
template <class T>
void foo() { ... }
template <class F>
void call_func_with(F f) {
f(tag_t<int>{} );
f(tag_t<double>{} );
}
call_with_func([](auto tag) { foo<decltype(tag)::type>(); } );
Here, f(tag_t<X>{} ) ends up calling foo<X>(), as desired.
Sometimes it works sometimes not:
template <class T>
void f(T t) {}
template <class T>
class MyClass {
public:
MyClass(T t) {}
};
void test () {
f<int>(5);
MyClass<int> mc(5);
f(5);
MyClass mc(5); // this doesn't work
}
Is there a way to hack around the example above? I.e. force the compiler to infer the template parameter from constructor parameter.
Will this be fixed in the future, or is there a good reason not to?
What is the general rule when compiler can infer template parameter?
Template parameters can be inferred for function templates when the parameter type can be deduced from the template parameters
So it can be inferred here:
template <typename T>
void f(T t);
template <typename T>
void f(std::vector<T> v);
but not here:
template <typename T>
T f() {
return T();
}
And not in class templates.
So the usual solution to your problem is to create a wrapper function, similar to the standard library function std::make_pair:
template <class T>
class MyClass {
public:
MyClass(T t) {}
void print(){
std::cout<<"try MyClass"<<std::endl;
}
};
template <typename T>
MyClass<T> MakeMyClass(T t) { return MyClass<T>(t); }
and then call auto a = MakeMyClass(5); to instantiate the class.
Read up on Template Argument Deduction (and ADL or Koenig lookup).
Since C++20, it is possible to infer the types of function parameters using auto:
#include <iostream>
#include <string>
auto print_stuff(auto x, auto y)
{
std::cout << x << std::endl;
std::cout << y << std::endl;
}
int main()
{
print_stuff(3,"Hello!");
print_stuff("Hello!",4);
return 0;
}