简体   繁体   English

为什么这段代码用g ++而不是MSVC ++编译?

[英]Why does this code compile with g++ but not MSVC++?

I'm trying to compile some open-source code ( https://github.com/BieremaBoyzProgramming/bbpPairings ), which I can get to compile on linux using g++ (v6.3.0), but which fails to compile in Visual Studio (VS Community 2019/16.1.5), with the somewhat obscure (to me, but my C++ is admittedly weak) error: "error C2143: syntax error: missing ';' 我正在尝试编译一些开源代码( https://github.com/BieremaBoyzProgramming/bbpPairings ),我可以使用g++ (v6.3.0)在linux上编译,但无法在Visual Studio中编译( VS社区2019 / 16.1.5),有些模糊(对我来说,但我的C ++确实很弱)错误:“错误C2143:语法错误:缺少';' before '<'". 在'<'“之前。

The offending code in the source is here , but a minimal example extracted from the code is: 源中的违规代码在这里 ,但从代码中提取的最小示例是:

#include <iostream>
#include <random>

class Configuration {};

class MatchesConfiguration {
public:
    template <class RandomEngine>
    MatchesConfiguration(
        Configuration&&,
        RandomEngine&);
};

template <class RandomEngine>
MatchesConfiguration::MatchesConfiguration(
    Configuration&& configuration,
    RandomEngine& randomEngine) {}

template
MatchesConfiguration::MatchesConfiguration<std::minstd_rand>( // <--- SYNTAX ERROR HERE
    Configuration&&,
    std::minstd_rand&);

int main()
{
    std::cout << "Hello World!\n"; 
}

I've had a look at the MSDN description of the error code , but my grasp of C++ and templates is too tenuous to figure out what's going wrong. 我已经看过错误代码MSDN描述了 ,但是我对C ++和模板的掌握太过于难以弄清楚出了什么问题。 The project README says that C++14 is expected (with some optional C++17 stuff for FS stuff which shouldn't matter here I think), but as far as I can make out from the feature compatibility chart all of C++14 should be supported by VS 2019. 项目自述文件说C ++ 14是预期的(有一些可选的C ++ 17东西用于FS的东西,这在我认为无关紧要),但据我可以从功能兼容性图表中得出所有的C + VS 2019应支持+14。

When you provide an explicit instantiation definition of a constructor (which does not really have a name according to the standard), you should do it by supplying the signature you'd like to instantiate, like so: 当您提供构造函数显式实例化定义 (根据标准没有真正的名称)时,您应该通过提供您想要实例化的签名来执行此操作,如下所示:

template
MatchesConfiguration::MatchesConfiguration(  // no <std::minstd_rand> here
    Configuration&&,
    std::minstd_rand&);

[ temp.arg.explicit#2 ] [ temp.arg.explicit#2 ]

Template arguments shall not be specified when referring to a specialization of a constructor template 在引用构造函数模板的特化时,不应指定模板参数


Trivia from an old note (from 2006): 来自旧笔记的琐事(自2006年起):
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#581 http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#581

" Can a templated constructor be explicitly instantiated or specialized? " 模板化构造函数可以显式实例化还是专门化?

it is not possible to specify a constructor's template arguments in a constructor invocation (because the constructor has no name but is invoked by use of the constructor's class's name) 在构造函数调用中无法指定构造函数的模板参数(因为构造函数没有名称,但是通过使用构造函数的类名称来调用)

[...] [...]

It was observed that explicitly specifying the template arguments in a constructor declaration is never actually necessary because the arguments are, by definition, all deducible and can thus be omitted. 据观察,在构造函数声明中明确指定模板参数实际上从来都不是必需的,因为根据定义,参数都是可推导的,因此可以省略。

Note that normal function templates can have non-deducible template parameters that must be supplied explicitly for an instantiation or specialization. 请注意,普通函数模板可以具有非可推导的模板参数, 必须为实例化或专门化明确提供这些参数。

Thanks to Davis Herring and MM for guidance 感谢Davis Herring和MM的指导

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM