3

bar假设我在另一个库中定义了以下代码:

void bar(int x);  /* this declaration comes from some included header file */

void foo(long long y)
{
    return bar(y);
}

为了使故障可检测,GSL 指示我gsl::narrow按如下方式使用:

void bar(int x);  /* this declaration comes from some included header file */

void foo(long long y)
{
    return bar(narrow<int>(y));
}

我的问题是,假设我知道void bar(long long x);可能会出现在该库的未来版本中,第一个版本会自动切换到使用它(通过推演规则),而第二个版本不会。有没有办法在void bar(long long x);不可用时检测缩小故障,并在释放时切换到仅调用它?

我试过了:

  • bar(narrow(y));,但无法推断出narrow的模板参数。
  • bar(superint{y}), 其中 superint 是 a对andstruct {long long x;}都具有重载类型转换运算符,后者具有窄检查。这是我最好的主意,因为它在添加时会给出编译时错误,让我们知道在合适的时候更新代码库的地方。不过,它看起来不像你会放在 GSL 中的东西,所以我不太满意..long longintambiguous callvoid bar(long long x);

更新

有很多好的答案,但它们都必须按函数而不是按参数来实现。理想情况下,解决方案应该类似于 GSL,您只需对有缩小风险的论点做一些事情。访问者模式在这里可能很有用,我们只需要重写quz(xi,yi,z,w)superint_visit(quz, superint{xi},superint{xi},z,w);假设 xi 和 yi 是有缩小风险的整数参数。就像是:

struct superint
{
    long long x;
    operator long long() { return x; }
    operator int()       { return narrow<int>(x); }
};

template <typename F, typename... Args>
auto superint_visit(F func, Args&&... args)
{
    /* if replacing 'superint' with 'long long' in Args 
       gives a function that is declared, call that function 
       (using static_cast<long long> on all 'superint' args 
       to resolve ambiguity). Otherwise, use static_cast<int> 
       on all 'superint' args and call that function instead. */
}

以上所有内容都可以存在于gsl命名空间中,我只需将我的函数编写为:

void foo(long long x)
{
    return superint_visit(bar, superint{x});
}

即使我在这里接受了答案,我仍然希望听到任何可以实现上述目标的人!

4

4 回答 4

2

您可以利用重载解析有利于非模板函数的参数类型完美匹配这一事实,而不是函数模板:

#include <iostream>

// (A)
void bar(int y) { std::cout << "bar(int)\n"; }
// (B)
//void bar(long long) { std::cout << "bar(long long)\n"; }

// (C)
template <typename T>
void bar(T) = delete;

// (C.SP1)
template<>
void bar<long long>(long long y) { bar(narrow<int>(y)); }

int main() {
    long long a = 12LL;
    bar(a); // bar(int)
            // bar(long long) if `bar(long long)` above is available.
}

如果void bar(long long);不可用,则对类型bar(a)参数的任何调用都将支持非窄化函数模板重载 (C),其主模板已被删除,仅允许通过专门化 (C.SP1)准确调用(无转换) )。一旦在 (B) 处可用,它将通过重载决议被选为比函数模板候选者更好的可行候选者。along longTlong longvoid bar(long long);

如果您担心在编译库本身时引入额外的bar重载(上面的函数模板)可能会破坏重载解析,您可以添加一个公共包装器并将重载解析委托放在上面定义包装器的 TU 中,应用通过在未命名的命名空间中声明添加的重载的内部链接。例如:barbarbar

// foo.h
#pragma once
void foo(long long y);

// foo.cpp
#include "foo.h"
#include "gsl/gsl_narrow"
#include "the_lib/bar.h"

namespace {

template <typename T>
void bar(T) = delete;

template<>
void bar<long long>(long long y) { bar(narrow<int>(y)); }

}  // namespace

void foo(long long y) {
    bar(y);
}
于 2020-08-21T10:58:00.527 回答
2

你不拥有bar?没问题。这是获取decltypefor的问题x。您可以通过获取decltype整个函数的 并编写 atemplate来恢复参数类型来做到这一点:

template <typename>
struct argType;

template <typename R, typename A>
struct argType<R(A)>
{
    using type = A;
};

void foo(long long y)
{     
    bar(narrow<typename argType<decltype(bar)>::type>(y));
}
于 2020-08-21T10:49:44.580 回答
1

可以使用boost::callable_traits::args获取第一个参数的类型,这将为您提供一个 包含所有参数类型的std::tuple,然后可以使用std::tuple_element获取第一个参数的类型

#include <boost/callable_traits/args.hpp>

void bar(int y);

void foo(long long y)
{
  bar(narrow<std::tuple_element<0, boost::callable_traits::args_t<decltype(bar)>>::type>(y));
}
于 2020-08-21T11:19:57.920 回答
0

这个问题已经发布了非常好的创造性解决方案。但是它们都有依赖于库实现的问题——例如。如果存在 bar 重载,则客户端代码不会编译。

在我看来,bar的客户在这种情况下真正想要的是:

“我想使用接受一个特定积分参数的栏,如果存在其他重载则无关”。

独立于库的非内在直接方式也是最简单的方式 - 一个带有整数参数的细条包装器。这样它就独立于任何库实现细节:

void bar(int x);  /* this declaration comes from some included header file */

template <typename T, typename = std::enable_if_v<std::is_integral_v<T>>>
inline void mybar(T x)
{
     // express directly which bar overload you want to use here 
     bar(narrow<int>(x));  // <- the only place you need to ajudst if bar interface changes, btw. use the overload from bar which you really want
}

void foo(long long y)
{
    return mybar(y);  // use mybar instead of bar within your code
}

如果存在 int 和 long 的 bar 重载,那么您可以通过简单地专门化您自己的 mybar 来区分这两者。

如果 bar 库接口发生变化,客户端无论如何都应该添加 apt 并重新编译。你真正想要的是将这些更改保持在中心位置。

在我看来,你已经或多或少地回答了你自己的问题。

于 2020-08-21T10:47:29.477 回答