[英]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
是參數是無法區分的。 如果通過callitA()
來查看c
和d
是右值,則需要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”是非常量的,因此我可以重載read函數以僅讀取此參數。
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.