强制从函数中推导模板以在适用的情况下生成 const 引用
Force template deduction from a function to produce const references where applicable
运行在 vscode 中使用 gdb 通过以下函数告诉我,T (*)(const int &, const int *, int &, int)
形式的函数推导的 argTypes 是 int
const int *
int &
和 int
分别。当出现 const Type &
参数时,有什么方法可以强制编译器推断出 const Type &
吗?或者是否有其他方法可以以有用的方式提取该类型信息?
#include<typeinfo>
template<typename T, typename...argTypes>
void testfunc(T (*f)(argTypes...))
{
const char *a[] = { typeid(argTypes).name()... };
for(auto &av :a)
{
std::cout << av << std::endl;
}
}
编辑:
更多上下文:这个函数显然几乎没有做任何事情,但产生它的问题函数也接受所有参数 运行 和 f ,而不是推导它们,而是转换它们。
这给不可复制的对象用作常量引用带来了问题。
使用testfunc
的例子如下:
#include "testfunc.h"
std::vector<bool> funcToTest(const int &a, const int *b, int &c, int d)
{
std::vector<bool> out;
out.push_back(&a == b);
out.push_back(&c == b);
out.push_back(&d == b);
return out;
}
int main()
{
// put a breakpoint here, and step in, you would see that 'a'
// describes the situation as described above.
testfunc(funcToTest);
}
这里的问题是 typeid
,而不是模板推导。如果你使用
template<typename... Ts>
struct types;
template<typename T, typename...argTypes>
void testfunc(T (*f)(argTypes...))
{
types<argTypes...>{};
}
您会收到一条不错的错误消息,例如
main.cpp: In instantiation of 'void testfunc(T (*)(argTypes ...)) [with T = std::vector<bool>; argTypes = {const int&, const int*, int&, int}]':
main.cpp:30:24: required from here
main.cpp:12:5: error: invalid use of incomplete type 'struct types<const int&, const int*, int&, int>'
12 | types<argTypes...>{};
| ^~~~~
main.cpp:7:8: note: declaration of 'struct types<const int&, const int*, int&, int>'
7 | struct types;
| ^~~~~
这表明函数参数类型已正确推导。
与 typeid
如果类型是引用,则它 returns 被引用的类型。它还会删除类型上的所有 cv 限定。也就是说
int main()
{
std::cout << typeid(int).name() << "\n";
std::cout << typeid(int&).name() << "\n";
std::cout << typeid(const int).name() << "\n";
std::cout << typeid(const int&).name() << "\n";
std::cout << typeid(volatile int).name() << "\n";
std::cout << typeid(volatile int&).name() << "\n";
std::cout << typeid(const volatile int).name() << "\n";
std::cout << typeid(const volatile int&).name() << "\n";
}
打印
i
i
i
i
i
i
i
i
运行在 vscode 中使用 gdb 通过以下函数告诉我,T (*)(const int &, const int *, int &, int)
形式的函数推导的 argTypes 是 int
const int *
int &
和 int
分别。当出现 const Type &
参数时,有什么方法可以强制编译器推断出 const Type &
吗?或者是否有其他方法可以以有用的方式提取该类型信息?
#include<typeinfo>
template<typename T, typename...argTypes>
void testfunc(T (*f)(argTypes...))
{
const char *a[] = { typeid(argTypes).name()... };
for(auto &av :a)
{
std::cout << av << std::endl;
}
}
编辑: 更多上下文:这个函数显然几乎没有做任何事情,但产生它的问题函数也接受所有参数 运行 和 f ,而不是推导它们,而是转换它们。 这给不可复制的对象用作常量引用带来了问题。
使用testfunc
的例子如下:
#include "testfunc.h"
std::vector<bool> funcToTest(const int &a, const int *b, int &c, int d)
{
std::vector<bool> out;
out.push_back(&a == b);
out.push_back(&c == b);
out.push_back(&d == b);
return out;
}
int main()
{
// put a breakpoint here, and step in, you would see that 'a'
// describes the situation as described above.
testfunc(funcToTest);
}
这里的问题是 typeid
,而不是模板推导。如果你使用
template<typename... Ts>
struct types;
template<typename T, typename...argTypes>
void testfunc(T (*f)(argTypes...))
{
types<argTypes...>{};
}
您会收到一条不错的错误消息,例如
main.cpp: In instantiation of 'void testfunc(T (*)(argTypes ...)) [with T = std::vector<bool>; argTypes = {const int&, const int*, int&, int}]':
main.cpp:30:24: required from here
main.cpp:12:5: error: invalid use of incomplete type 'struct types<const int&, const int*, int&, int>'
12 | types<argTypes...>{};
| ^~~~~
main.cpp:7:8: note: declaration of 'struct types<const int&, const int*, int&, int>'
7 | struct types;
| ^~~~~
这表明函数参数类型已正确推导。
与 typeid
如果类型是引用,则它 returns 被引用的类型。它还会删除类型上的所有 cv 限定。也就是说
int main()
{
std::cout << typeid(int).name() << "\n";
std::cout << typeid(int&).name() << "\n";
std::cout << typeid(const int).name() << "\n";
std::cout << typeid(const int&).name() << "\n";
std::cout << typeid(volatile int).name() << "\n";
std::cout << typeid(volatile int&).name() << "\n";
std::cout << typeid(const volatile int).name() << "\n";
std::cout << typeid(const volatile int&).name() << "\n";
}
打印
i
i
i
i
i
i
i
i