模板化 using 无法选择模板函数用作 Visual Studio 中的参数

Templated usings Can#39;t Select Template Functions to use as Parameters in Visual Studio(模板化 using 无法选择模板函数用作 Visual Studio 中的参数)

本文介绍了模板化 using 无法选择模板函数用作 Visual Studio 中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是尽可能简化的,我可以制作一个仍然遇到错误的玩具示例:

This is about as simplified as I could make a toy example that still hit the bug:

struct Vector3f64 {
  double x;
  double y;
  double z;
};

struct Vector3f32 {
  float x;
  float y;
  float z;
};

// I use this to select their element type in functions:
template <typename T>
using param_vector = std::conditional_t<std::is_same_v<std::remove_const_t<std::remove_reference_t<T>>, Vector3f64>, double, float>;

// This is the function I want to pull the return type from:
template <typename T>
T VectorVolume(const T x, const T y, const T z) {
  return x * x + y * y + z * z;
}

template<class R, class... ARGS>
std::function<R(ARGS...)> make_func(R(*ptr)(ARGS...)) {
  return std::function<R(ARGS...)>(ptr);
}

// This function fails to compile:
template <typename T>
typename decltype(make_func(&VectorVolume<param_vector<T>>))::result_type func(const T& dir) {
  return VectorVolume(dir.x, dir.y, dir.z);
}

int main() {
  const Vector3f64 foo{ 10.0, 10.0, 10.0 };

  std::cout << func(foo) << std::endl;
}

make_func 来自 SergyA 的回答,我想创建一个 std::function 所以我可以找到返回类型,而无需显式声明 VectorVolume 采用的参数.但我从 visual-studio-2017 版本 15.6.7:

The make_func is from SergyA's answer which I wanted to create a std::function so I could find a return type without explicitly declaring the parameters VectorVolume took. But I get this error from visual-studio-2017 version 15.6.7:

错误 C2039:result_type:不是全局命名空间"的成员错误 C2061:语法错误:标识符 func错误 C2143:语法错误:在 { 之前缺少 ;错误 C2447:{:缺少函数头(旧式正式列表?)错误 C3861:func:未找到标识符

error C2039: result_type: is not a member of 'global namespace' error C2061: syntax error: identifier func error C2143: syntax error: missing ; before { error C2447: {: missing function header (old-style formal list?) error C3861: func: identifier not found

这适用于 c++14 in g++: https://ideone.com/PU3oBV 它甚至可以工作visual-studio-2017 如果我没有将 using 语句作为模板参数传递:

This works fine on c++14 in g++: https://ideone.com/PU3oBV It'll even work fine on visual-studio-2017 if I don't pass the using statement as a template parameter:

template <typename T>
typename decltype(make_func(&VectorVolume<double>))::result_type func(const T& dir) {
    return VectorVolume(dir.x, dir.y, dir.z);
}

这与我在这里解决的问题几乎相同:Templated usings Can't be Nested in Visual Studio 不幸的是,在这种情况下,我可以用 result_of 调用替换我的函数构造.在这种情况下,我只是不知道如何重新设计 make_func 来解决这个错误.有谁知道解决方法?(除了升级到 15.9.5,它确实解决了这个问题.)

This is almost identical to the problem I worked around here: Templated usings Can't be Nested in Visual Studio Unfortunately, in that case I could just replace my function construction with a result_of call. In this case I just don't see how I can redesign make_func to work around this bug. Does anyone know of a workaround? (Other than upgrading to 15.9.5, which does solve this.)

推荐答案

你真的只对 function::result_type 感兴趣,所以真的没有必要通过返回一个错误的路径功能.只需返回结果类型并对其执行 decltype (您甚至不需要定义函数,因为您实际上并没有调用它.)像这样:

You're really only interested in the function::result_type so there's really no need to go through the bugged path of returning a function. Just return the result type and do a decltype on that (you don't even need to define the function since you're not actually calling it.) Something like this:

template <typename R, typename... ARGS>
R make_func(R(*)(ARGS...));

那么直接使用返回类型即可:

Then just directly use the return type:

template <typename T>
decltype(make_func(&VectorVolume<param_vector<T>>)) func(const T& dir) {
    return VectorVolume(dir.x, dir.y, dir.z);
}

这在 Visual Studio 15.6.7 上效果很好,而且额外的好处是完全 c++14 兼容:https://ideone.com/gcYo8x

This is works great on Visual Studio 15.6.7 and as an added bonus is fully c++14 compatible: https://ideone.com/gcYo8x

这篇关于模板化 using 无法选择模板函数用作 Visual Studio 中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:模板化 using 无法选择模板函数用作 Visual Studio 中的参数

基础教程推荐