gpt4 book ai didi

c++ - 非标准代码,还是 g++ 错误?

转载 作者:太空宇宙 更新时间:2023-11-04 11:58:06 25 4
gpt4 key购买 nike

好的,我已经开发了一些代码;

Linkage (有点长)

当我用 Clang++ 3.2 编译它时,它运行并产生;

stdout: 
print: C-3PO
print: R2D2

但是,如果我尝试使用 G++ 4.7.2 编译它,则会出现这些错误;

Compilation finished with errors:
source.cpp: In function 'int main()':
source.cpp:90:71: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char, int)'
source.cpp:90:71: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note: template argument deduction/substitution failed:
source.cpp:90:71: note: mismatched types 'const char*' and 'char'
source.cpp:90:71: error: unable to deduce 'auto' from '<expression error>'
source.cpp:92:72: error: no matching function for call to 'makeRunnable(int (&)(char, int, const char*), char)'
source.cpp:92:72: note: candidate is:
source.cpp:74:27: note: template<class ... RUN_TIME, class T, class ... CONSTRUCTION_TIME> Runnable<T, RUN_TIME ...>* makeRunnable(T (*)(CONSTRUCTION_TIME ..., RUN_TIME ...), CONSTRUCTION_TIME ...)
source.cpp:74:27: note: template argument deduction/substitution failed:
source.cpp:92:72: note: mismatched types 'int' and 'char'
source.cpp:92:72: error: unable to deduce 'auto' from '<expression error>'

与 G++ 4.8.0 几乎相同(尽管格式更漂亮)。

所以问题是;

这段代码是否符合标准? - 如果不是,为什么?

编辑链接中的相关代码:

template<typename... RUN_TIME, typename T, typename... CONSTRUCTION_TIME> 
Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)(CONSTRUCTION_TIME..., RUN_TIME...), CONSTRUCTION_TIME... ct_args) // Line 74
{
return new FunctionDelegate<T,
std::tuple<CONSTRUCTION_TIME...>,
std::tuple<CONSTRUCTION_TIME..., RUN_TIME...>,
RUN_TIME...>(FunctionType, std::make_tuple(ct_args...));
}

int print_function(char arg1, int arg2, const char* arg3)
{
std::cout << "print: " << arg1 << arg2 << arg3 << std::endl;
return 2;
}

int main()
{
auto function1 = makeRunnable<const char*>(print_function, 'C', -3); // Line 90
int n = function1->invoke("PO");
auto function2 = makeRunnable<int, const char*>(print_function, 'R'); // Line 92
function2->invoke(n, "D2");
}

这个问题的重点并不是真正有问题的实现,更多的是 Clang++ 和 G++ 在这是否是一个错误上没有分歧。

最佳答案

稍微研究一下代码,g++ 似乎无法处理可变参数模板连接的推导(在 |!| 之间)。

Runnable<T, RUN_TIME...>* makeRunnable(T (*FunctionType)|!|(CONSTRUCTION_TIME..., RUN_TIME...)|!|, CONSTRUCTION_TIME... ct_args)

并且代码是可修复的,通过添加另一个可变模板参数,这样 g++ 就可以直接推导模板。

关于c++ - 非标准代码,还是 g++ 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15270998/

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