我正在尝试编译一些开源代码(https://github.com/BieremaBoyzProgramming/bbpPairings),我可以使用g++
(v6.3.0)在Linux上进行编译,但是无法在Visual Studio中进行编译(VS Community 2019 / 16.1.5),但有些模糊(对我来说,但是我的C ++确实很弱)错误:“错误C2143:语法错误:缺少';'在“ <””之前。
源代码中令人反感的代码是here,但是从代码中提取的一个最小示例是:
#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";
}
我看过MSDN description of the error code,但是我对C ++和模板的理解太微不足道,无法找出问题所在。项目README表示,预期会使用C ++ 14(对于FS来说有一些可选的C ++ 17东西,我认为这在这里不重要),但是据我所知,feature compatibility chart VS 2019应该支持C ++ 14。
答案 0 :(得分:5)
当您提供explicit instantiation definition中的constructor(根据标准并没有真正的名称)时,应通过提供要实例化的签名来实现,例如:
template
MatchesConfiguration::MatchesConfiguration( // no <std::minstd_rand> here
Configuration&&,
std::minstd_rand&);
引用参数时,不得指定模板参数 构造器模板的专业化
旧笔记中的琐事(从2006年开始):
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#581
“ 可以显式实例化或专用化模板化构造函数吗?”
不能在对话框中指定构造函数的模板参数 构造函数调用(因为构造函数没有名称,但是 通过使用构造函数的类的名称调用)
[...]
已经观察到,实际上不需要在构造函数声明中明确指定模板参数,因为根据定义,这些参数都是可推导的,因此可以省略。
请注意,普通功能模板可以具有不可推导的模板参数,必须为实例化或专业化明确提供 。
感谢Davis Herring和M.M的指导