我正在研究一种使用多重继承而不是经典递归定义的元组类型。这样做时,我在扩展多个参数包时遇到了一个奇怪的问题,这些参数包根据 clang 具有不同的长度,而 gcc 编译代码没有问题。
可以在此处找到一个演示该问题的小示例:https : //godbolt.org/z/oKbYKd9je
使用 clang 12.0.1 编译时出现错误:
包扩展包含与外部参数包具有不同长度(3 对 1)的参数包“Ts”
切换到 gcc 11 时,代码编译没有问题。我想知道哪个编译器是正确的?对我来说,这似乎应该可以正常工作并且错误在叮当声中。
此处还包含代码,以防外部链接过期:
#include <type_traits>
#include <utility>
template <size_t index, typename T>
struct element_holder {
T value;
};
template <typename... Ts>
struct tuple : public Ts... {};
namespace detail{
template<typename T>
struct make_tuple_impl2;
template<size_t...Is>
struct make_tuple_impl2<std::index_sequence<Is...>>{
template<typename ...Ts>
using f = tuple<element_holder<Is, Ts>...>; //<-- error occurs here
};
template<size_t n>
struct make_tuple_impl{
template<typename... Ts>
using f=typename make_tuple_impl2<std::make_index_sequence<n>>::template f<Ts...>;
};
}
struct make_tuple{ …Run Code Online (Sandbox Code Playgroud) 我试图在编译时验证给定的 lambda 是否接受某种类型(在我的示例代码中为双倍)。只要 lambda 的签名明确指定了它的工作类型。但是,一旦我在签名中使用带有 auto 的通用 lambda,在评估 requires 语句时就会出现编译错误。
下面的代码片段说明了这个问题(也在编译器资源管理器上)
#include <concepts>
#include <iostream>
#include <string>
template<typename F>
concept accepts_double = requires(F f, double d) { f(d); };
struct Component{};
int main(){
auto f1 = [](double a){double b = a;};
auto f2 = [](std::string a){std::string b = a;};
auto f3 = [](auto a){std::string b = a;};
std::cout << std::boolalpha << accepts_double<decltype(f1)> << "\n"; // expected true
std::cout << std::boolalpha << accepts_double<decltype(f2)> << "\n"; // expected false
//This one …Run Code Online (Sandbox Code Playgroud)