как избежать static_cast при использовании std :: make_tuple с перегруженными функциями

g ++ говорит

ошибка: слишком много аргументов для функции constexpr std :: tuple

если я оставлю static_cast в вызове std::make_tuple

#include <tuple>

typedef int (*func_t)();

int number() {
  return 2;
}

double number(bool a) {
  return 1.2;
}

int main() {
  // With a static_cast it compiles without any error
  // std::tuple<func_t> tup = std::make_tuple(static_cast<func_t>(number));                                                               

  std::tuple<func_t> tup = std::make_tuple(number);
  return 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.

Если я изменю основную функцию на

int main() {
  std::tuple<func_t> tup = std::make_tuple(static_cast<func_t>(number));                                                               
  return 0;
}

программа компилируется нормально. Можно ли как-нибудь исключить static_cast? Кажется ненужным указывать тип func_t два раза.


person Erik Sjölund    schedule 11.01.2013    source источник
comment
Попробуйте std :: make_tuple ‹func_t› ()   -  person dans3itz    schedule 11.01.2013
comment
Как вы думаете, какую функцию int number() или double number(bool a) следует передать std::make_tuple(number);? Вы можете попробовать так: std::tuple<func_t> tup( (number) );   -  person borisbn    schedule 11.01.2013


Ответы (1)


Не используйте std::make_tuple. Используйте braced-init-list как:

std::tuple<func_t> tup { number };

Теперь компилятор выберет соответствующую перегрузку, совпадающую с func_t.

См. живую демонстрацию

person Nawaz    schedule 11.01.2013
comment
Можете ли вы удалить ссылку на «живую демонстрацию». - person dans3itz; 11.01.2013
comment
Это просто пример того, где тестировать код? Не уверен, для чего нужна ссылка. - person dans3itz; 11.01.2013