gpt4 book ai didi

c++ - 需要引用标准关于 main 函数作为模板函数的合法性

转载 作者:可可西里 更新时间:2023-11-01 18:29:46 25 4
gpt4 key购买 nike

一时兴起,我尝试使用 clang 2.9 将 main 函数定义为模板函数:

template <typename T = void>
int main(int argc, char **argv)
{
}

并收到以下错误。

error: 'main' cannot be a template
int main(int argc, char **argv)
^

有谁知道标准的哪一部分禁止这样做,相关文本是什么?

最佳答案

那么,这个 (3.6.1) 怎么样:

A program shall contain a global function called main, which is the designated start of the program. [...] This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined.

由于模板不是函数,我认为您在这件事上别无选择。特别是,函数必须是 main , 不是 main<>就像你的例子一样;和你的 main不是一个函数,而是一个模板,排除了另一个名为 main 的函数的存在.

关于c++ - 需要引用标准关于 main 函数作为模板函数的合法性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8146647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com