如何解决与GCC / Clang(无法识别的模板定义)不同的C ++函数模板的MSVC处理?

问题描述 投票:3回答:1

存在模板助手功能

#include <iostream>

namespace helpers {

  template <typename OUT = float, typename IN>
  OUT normalizeFromRange(IN v, IN min, IN max) {
    return static_cast<OUT>(v - min) / static_cast<OUT>(max - min);
  }

}

int main()
{
    float resultFAuto = helpers::normalizeFromRange<>(10, 0, 40);
    float resultF = helpers::normalizeFromRange<float>(20, 0, 40);
    double resultD = helpers::normalizeFromRange<double>(30, 0, 40);

    std::cout << "resultFAuto: " << resultFAuto << std::endl;
    std::cout << "resultF: " << resultF << std::endl;
    std::cout << "resultD: " << resultD << std::endl;

    return 0;
}

结果是

resultFAuto: 0.25
resultF: 0.5
resultD: 0.75

它编译在Clang,GCC,这是我使用的编译器。 C ++ 14。我想在MSVC ++和它声称使用compiles on online MSVC++ compiler19.00.23506上构建项目

但它在使用19.16.27027.1,C ++ 14 set的机器上失败了。

Microsoft Visual Studio Community 2017 
Version 15.9.9
VisualStudio.15.Release/15.9.9+28307.518
Installed Version: Community
Microsoft Visual C++ 2017
Microsoft (R) C/C++ Optimizing Compiler Version 19.16.27027.1

error C2988: unrecognizable template declaration/definition
error C2059: syntax error: '('
error C2143: syntax error: missing ';' before '{'
error C2447: '{': missing function header (old-style formal list?)

模板函数是否使用不正确的语法定义(并且GCC,Clang,某些MSVC ++版本只是以某种方式通过)? 或者它是MSVC特定的,MSVC标志特定的,或最近的MSVC具体? 别的什么? 有one similar issue,但我对模板的了解不允许看到它在这里如何应用。

c++ templates visual-c++ visual-studio-2017
1个回答
4
投票

你的项目是否包括windows.h

包括windows.h也带来了OUTIN的预处理器定义,我认为这导致了这个错误。

将模板类型名称更改为OUTXINX可修复我的计算机上的错误。

我发现了一个相关的SO问题:IN and OUT macros in minwindef.h

© www.soinside.com 2019 - 2024. All rights reserved.