Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在使用带有重载函数的std::make_tuple时避免静态_强制转换_C++_C++11_Tuples - Fatal编程技术网

C++ 如何在使用带有重载函数的std::make_tuple时避免静态_强制转换

C++ 如何在使用带有重载函数的std::make_tuple时避免静态_强制转换,c++,c++11,tuples,C++,C++11,Tuples,g++说 错误:函数“constexpr std::tuple”的参数太多 如果我在std::make\u tuple调用中省略了static\u cast #包括 typedef int(*func_t)(); 整数(){ 返回2; } 双倍数字(布尔a){ 回报率1.2; } int main(){ //通过静态_cast,它编译时不会出现任何错误 //std::tuple tup=std::make_tuple(static_cast(number)); std::tuple tup=st

g++说

错误:函数“constexpr std::tuple”的参数太多

如果我在
std::make\u tuple
调用中省略了static\u cast

#包括
typedef int(*func_t)();
整数(){
返回2;
}
双倍数字(布尔a){
回报率1.2;
}
int main(){
//通过静态_cast,它编译时不会出现任何错误
//std::tuple tup=std::make_tuple(static_cast(number));
std::tuple tup=std::make_tuple(数字);
返回0;
}
以下是完整的错误消息:

$ g++ -std=c++11 test.cc
test.cc: In function 'int main()':
test.cc:31:54: error: too many arguments to function 'constexpr std::tuple<typename std::__decay_and_strip<_Elements>::__type ...> std::make_tuple(_Elements&& ...) [with _Elements = {}; typename std::__decay_and_strip<_Elements>::__type = <type error>]'
In file included from test.cc:1:0:
/usr/include/c++/4.7/tuple:844:5: note: declared here
test.cc:31:54: error: conversion from 'std::tuple<>' to non-scalar type 'std::tuple<int (*)()>' requested
$ g++ --version
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$g++-std=c++11 test.cc
test.cc:在函数“int main()”中:
test.cc:31:54:错误:函数“constexpr std::tuple std::make_tuple(_Elements&&…[带_Elements={};typename std::_decation_和_strip::_类型=]”的参数太多
在test.cc中包含的文件中:1:0:
/usr/include/c++/4.7/tuple:844:5:注意:在这里声明
test.cc:31:54:错误:请求从“std::tuple”转换为非标量类型“std::tuple”
$g++--版本
g++(Ubuntu/Linaro 4.7.2-2ubuntu1)4.7.2
版权所有(C)2012免费软件基金会。
这是自由软件;有关复制条件,请参见源。没有
担保甚至不是为了适销性或适合某一特定目的。
如果我将主函数更改为

intmain(){
std::tuple tup=std::make_tuple(static_cast(number));
返回0;
}

这个程序编译得很好。是否有可能以某种方式忽略静态的_投射?似乎没有必要提供两次类型func\t

不要使用
std::make\u tuple
。将带大括号的初始列表用作:

std::tuple tup{number};
现在,编译器将选择适当的重载匹配
func\t


请参见

Try std::make_tuple(),您认为应该向
std::make_tuple(number)传递什么函数
int number()
double number(bool a)
?您可以这样尝试:
std::tuple tup((number))你能删除“实时演示”链接吗?这只是测试代码的一个例子吗?不确定链接的用途。
std::tuple<func_t> tup { number };