获取模板以推断函数参数的确切类型
Get template to deduce the exact type of a functions parameter
我在获取模板来推断函数参数的确切类型时遇到问题。
在此示例中,调用 callit 不会推断出我想要的参数类型。callitA 的第一个调用将推导出 [int, int, int, int]。callitB 的第二次调用将推导出 [const int &, int &, const int &, int &]。只有当我对模板进行特定实例化时,我才能获得正确的参数。
如何在不明确指定模板参数的情况下获得下面 3 中的行为。它们应该可以从函数参数中推导出来。
提前谢谢。
void read() {}
template< typename P, typename... Args >
void read(const P & p, Args&... args) {
// p is a constant and shall not be written to.
read(args...);
}
template< typename P, typename... Args >
void read(P & p, Args&... args) {
cin >> p;
read(args...);
}
template< typename... Args >
void callitA(Args... args) {
read( args... );
}
template< typename... Args >
void callitB(Args&... args) {
read(args...);
}
// b is the only variable that can be returned from funk.
void funk(const int & a, int & b, const int c, int d) {
callitA(a, b, c, d); // 1. Args will be [int, int, int, int]
callitB(a, b, c, d); // 2. Args will be [const int &, int &, const int &, int &]
// 3. Here Args will be what I want [const int &, int &, const int, int]
callitA<const int &, int &, const int, int>(a, b, c, d);
}
void main() {
const int a = 1;
int b = 0;
const int c = 3;
int d = 4;
funk(a, b, c, d);
cout << b << endl;
}
在你的函数func()
所有四个参数都是左值,c
和d
是参数是无法区分的。如果你的意思是c
和d
在被callitA()
看时是右值,你需要std::move()
它们。
下面是一个程序,它在推导传递的参数方面做得更好:
#include <iostream>
void read() {
std::cout << 'n';
}
template <typename T>
struct type_to_string;
template <>
struct type_to_string<int&> {
static constexpr char const* value = "int&";
};
template <>
struct type_to_string<int const&> {
static constexpr char const* value = "int const&";
};
template <>
struct type_to_string<int> {
static constexpr char const* value = "int";
};
template <>
struct type_to_string<int const> {
static constexpr char const* value = "int const";
};
template< typename P, typename... Args >
void read(P&&, Args&&... args) {
std::cout << type_to_string<P>::value << ", ";
read(std::forward<Args>(args)...);
}
template< typename... Args >
void callitA(Args... args) {
read( args... );
}
template< typename... Args >
void callitB(Args&... args) {
read(args...);
}
template< typename... Args >
void callitC(Args&&... args) {
read(std::forward<Args>(args)...);
}
void funk(const int & a, int & b, const int c, int d) {
callitA(a, b, c, d);
callitB(a, b, c, d);
callitC(a, b, std::move(c), std::move(d));
}
int main() {
const int a = 1;
int b = 0;
const int c = 3;
int d = 4;
funk(a, b, c, d);
callitC(a, b, static_cast<int const>(3), 4);
}
感谢您的所有答案。里面有很多有趣的东西。
不幸的是,这是我似乎能够做我想做的事的唯一方法。
下面的程序产生以下输出:
int const, int, int const, int const,
int const, int, int const, int const,
重要的是,只有第二个参数"b"是非常量参数,因此我可以重载读取函数以仅读取此参数。
void read() {
std::cout << 'n';
}
template <typename T>
struct type_to_string;
template <>
struct type_to_string<int&> {
static char const* value;
};
char const* type_to_string<int&>::value = "int&";
template <>
struct type_to_string<int const&> {
static char const* value;
};
char const* type_to_string<int const&>::value = "int const&";
template <>
struct type_to_string<int> {
static char const* value;
};
char const* type_to_string<int>::value = "int";
template <>
struct type_to_string<int const> {
static char const* value;
};
char const* type_to_string<int const>::value = "int const";
template< typename P, typename... Args >
void read(P&, Args&... args) {
cout << type_to_string<P>::value << ", ";
read( args...);
}
template< typename... Args >
void callitC(Args&... args) {
read( args...);
}
// b is the only variable that can be returned from funk.
void funkA(const int & a, int & b, const int c, int d) {
callitC(a, b, c, const_cast<const int &>(d));
}
// b is the only variable that can be returned from funk.
void funkB(const int & a, int & b, const int c, int d) {
callitC<const int &, int &, const int, const int>(a, b, c, d);
}
void main() {
const int a = 1;
int b = 0;
const int c = 3;
int d = 4;
funk(a, b, c, d);
cout << endl;
funkB(a, b, c, d);
}
相关文章:
- 扩展C++生成的代码的模板参数类型名称
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 在 C++ 中运行时调用模板时,是否可以切换模板的参数类型?
- 将函数参数类型声明为 auto
- 将函数的参数 - 签名从使用 'std::function<T>' 转换为模板参数类型
- 在 C++17 中调用具有不同参数类型的构造函数
- 具有先前参数类型匹配的参数包
- 我想知道为什么"std::unique_ptr<int> foo(新 int)"是合法的,因为"std::<int>unique_ptr"要求输入参数类型应该是"int"?
- 将可变参数类型列表的扩展打包为复杂类型的初始值设定项列表 - 合法吗?
- MSVC 错误:4 个重载中的任何一个都无法转换所有参数类型
- 使用constexpr + auto作为返回和参数类型的奇怪类型推导
- 如何从第一个参数推断第二个参数类型?
- C++模板函数中,指定回调函子/lambda 的参数类型,同时仍允许内联?
- 如何用不同的参数类型和数字回调函数
- C++stoi:这两个重载都无法转换所有参数类型
- 为什么std::{container}::template不能推导其参数类型
- 为模板参数类型中的新对象分配内存
- 为指向成员模板参数的指针推导额外模板参数类型的紧凑方式
- 使用std::conditional中的模板来确定函数参数类型
- C++中的短参数类型