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

时间:2013-03-07 12:14:51

标签: 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 ++对于这是否是错误没有异议。

1 个答案:

答案 0 :(得分:1)

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

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

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