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

标签 c++ gcc c++11 clang

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

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/

相关文章:

c++ - 最终类的用例

c++ - 在类之间共享成员函数

c++ - xcb 鼠标移动导致输入滞后

c++ - 流出字符串的一部分

c - 在 Code::Blocks 中使用非标准函数

c - Linux中C语言中Printf()在 "while(1)"循环之前不执行

c++ - 右值、左值和正式定义

c++ - 如何使用 (<,>) 比较字符串

C++ 循环依赖 : What should the constructor look like?

c++ - 无法在 Linux 上使用 mongo-cxx-driver 的静态库