【发布时间】:2017-06-07 18:50:36
【问题描述】:
我正在尝试为竞争性编程竞赛编写自己的库,我需要这样的代码:
#include <functional>
#include <algorithm>
template <typename T>
using binop = std::function<T (T, T)>;
int main()
{
binop<int> op = std::max<int>;
}
不幸的是,它会产生以下错误:
error: conversion from '<unresolved overloaded function type>' to non-scalar type 'binop<int> {aka std::function<int(int, int)>}' requested
但是当我删除线时
#include <algorithm>
它神奇地编译。 (虽然实际上不应该定义一个 max 函数)
问题是:如何在不删除“算法”的情况下编译代码?
请注意,我也试过这个:
binop<int> op = (int(*)(int, int)) std::max<int>;
产生
error: insufficient contextual information to determine type
【问题讨论】:
-
有多个
std::max重载。有一个需要std::initializer_list -
我知道这一点,但是为什么 (int(*)(int, int)) std::max
不起作用呢? -
试试
(const int& (*)(const int&, const int&))。那是std::max<int>的实际签名 -
@VolkovKomm 您在哪里看到应该有这样的过载?我的文档显示不可能 int(int,int)。
-
哦,实际上没有 int(int, int) 重载...而且 ((const int&(*)(const int&, const int&)) 刚刚工作。我想我只是愚蠢,谢谢你的解释。
标签: c++ c++11 c++-standard-library