1

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

联动(相当长)

当我用 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++ 在这是否是错误方面没有分歧。

4

1 回答 1

1

稍微玩过代码后,似乎 g++ 无法处理可变参数模板连接的推导(在|!|.

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

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

于 2013-03-07T13:46:23.123 回答