小编W.F*_*.F.的帖子

模板继承可以从它的另一个特化是一个循环依赖,还是一个clang的bug

考虑以下代码:

#include <iostream>
#include <utility>
#include <tuple>

template <class TargetIndices, class SourceIndices>
struct assign;

template <size_t... TargetIs, size_t... SourceIs>
struct assign<std::index_sequence<TargetIs...>, std::index_sequence<SourceIs...>>: assign<std::index_sequence<TargetIs>, std::index_sequence<SourceIs>>... {
   template <class TargetTuple, class SourceTuple>
   assign(TargetTuple &target, const SourceTuple &source): assign<std::index_sequence<TargetIs>, std::index_sequence<SourceIs>>(target, source)... { }
};

template <size_t FirstTI, size_t FirstSI>
struct assign<std::index_sequence<FirstTI>, std::index_sequence<FirstSI>> {
   template <class TargetTuple, class SourceTuple>
   assign(TargetTuple &target, const SourceTuple &source) {
      std::get<FirstTI>(target) = std::get<FirstSI>(source);
   }
};


int main() {
   std::tuple<int, int, int> t1 = std::make_tuple(0,0,0), t2 = std::make_tuple(1,2,3);
   assign<std::index_sequence<0,2>, std::index_sequence<2,0>>(t1, …
Run Code Online (Sandbox Code Playgroud)

c++ templates circular-dependency variadic-templates c++14

6
推荐指数
1
解决办法
485
查看次数

两个指针的区别是合法的c ++ 17常量表达式吗?

根据cppreference部分核心常量表达式指出19)两个指针之间的减法运算符不是合法的常量表达式,直到c ++ 14.我可以假设以下代码是合法的c ++ 17代码或者这种解释是滥用吗?

int X, Y;

template <long long V>
struct S { };

int main() {
    S<&X - &Y> s;
    (void)s;
}
Run Code Online (Sandbox Code Playgroud)

c++ language-lawyer constant-expression c++17

6
推荐指数
1
解决办法
468
查看次数

逗号运算符使lambda表达式非constexpr

根据[此问答],因为c ++ 11逗号运算符是constexpr能力的.根据[此问答], constexpr变量不应该被lambda捕获,但应该可以在其体内使用.

这两条规则都使得以下代码可以在clang中编译:

//Example 1

template <int>
struct Foo {};

int main() {
    constexpr int c = 1;
    static_cast<void>(Foo<(c, 2)>{});
}
Run Code Online (Sandbox Code Playgroud)
//Example 2

template <int>
struct Foo {};

int main() {
    constexpr int c = 1;
    auto lambda = []{return c * 2;};
    static_cast<void>(Foo<lambda()>{});
}
Run Code Online (Sandbox Code Playgroud)

然而,虽然这两个例子在clang上成功编译(声明constexpr lambda支持是 - 8.0.0),但下面的代码片段没有,我无法想象为什么......任何想法?

template <int>
struct Foo {};

int main() {
    constexpr int c = 1;
    auto lambda = []{return (c, 2);};
    static_cast<void>(Foo<lambda()>{});
}
Run Code Online (Sandbox Code Playgroud)

编译错误:

变量'c'不能在没有指定capture-default的lambda中隐式捕获

[现场演示]

c++ lambda language-lawyer constexpr c++17

6
推荐指数
1
解决办法
268
查看次数

是否可以调用模板化的强制转换操作符显式指定模板参数?

考虑一下代码:

#include <string>
#include <sstream>

template <class T>
struct converter_impl {
   std::string to_convert;
   operator T() {
      T result;
      std::stringstream ss(to_convert);
      ss >> result;
      return result;
   }
};

struct converter {
   std::string to_convert;
   template <class T, class CI = converter_impl<T>>
   operator T() {
      CI ci = CI{std::move(to_convert)};
      return ci;
   }
};

converter from_string(std::string s) {
   return converter{std::move(s)};
}
Run Code Online (Sandbox Code Playgroud)

现在我可以使用from_string如下函数:

string s = "123";
int x = from_string(s);
cout << x << endl;
Run Code Online (Sandbox Code Playgroud)

我只是好奇是否有办法调用converterstruct 的强制转换操作符显式指定模板参数.语法:

from_string(s).operator int<int, converter_impl<int>>(); …
Run Code Online (Sandbox Code Playgroud)

c++ templates operator-overloading c++11

5
推荐指数
1
解决办法
240
查看次数

扩展非类型参数包以使用非类型模板参数定义内部类模板是否合法?

我能够生成的最小代码来重现问题:

template <int>
struct Tag { };

Tag<0> w;

template <int... Is>
struct Outer {
   template <Tag<Is> &...>
   struct Inner {
   };
};

int main() {
   Outer<0>::Inner<w> f;
}
Run Code Online (Sandbox Code Playgroud)

g ++(版本6.1.1 20160511)在编译代码时遇到以下错误:

pp.cc: In function ‘int main()’:
pp.cc:14:21: internal compiler error: unexpected expression ‘Is’ of kind template_parm_index
    Outer<0>::Inner<w> f;
Run Code Online (Sandbox Code Playgroud)

并产生长而无聊的堆栈跟踪.版本3.6.0中的clang ++似乎没有编译代码的任何问题.具有类型模板参数的相同代码在两个编译器中编译得很好:

template <class>
struct Tag { };

Tag<int> w;

template <class... Ts>
struct Outer {
   template <Tag<Ts> &...>
   struct Inner {
   };
};

int main() {
   Outer<int>::Inner<w> f;
}
Run Code Online (Sandbox Code Playgroud)

那么这是一个g …

c++ templates non-type variadic-templates c++14

5
推荐指数
1
解决办法
167
查看次数

我可以将引用类型传递给模板以指定以下非类型模板参数的类型吗?

考虑一个例子:

#include <type_traits>

template <class T, T>
struct has_duplicates_info { };

template <class T, T...>
struct has_duplicates;

template <class T, T First, T... Others>
struct has_duplicates<T, First, Others...>:
          has_duplicates<T, Others...>,
          has_duplicates_info<T, First> {
   static constexpr bool value =
      std::is_base_of<has_duplicates_info<T, First>, has_duplicates<T, Others...>>::value
        || has_duplicates<T, Others...>::value;
};

template <class T, T Last>
struct has_duplicates<T, Last>: has_duplicates_info<T, Last>, std::false_type { };

int a, b;

int main() {
   static_assert(!has_duplicates<int, 0, 1, 2>::value, "has_duplicates<int, 0, 1, 2>::value");
   static_assert(has_duplicates<int, 1, 2, 2, 3>::value, "!has_duplicates<int, 1, …
Run Code Online (Sandbox Code Playgroud)

c++ templates non-type c++14

5
推荐指数
1
解决办法
602
查看次数

与Clang的__type_pack_element等效的GCC(在任何版本中),以获取模板参数包的第N个元素

https://reviews.llvm.org/D15421

clang具有__type_pack_element允许在可变参数模板中对参数包进行有效索引的功能。是否有等效的GCC?

我对使用不感兴趣tuple_element_t。我正在寻找一种替代方法,它是编译器原语

c++ gcc intrinsics variadic-templates

5
推荐指数
1
解决办法
454
查看次数

是否有std :: unique_ptr <std :: array <T,N >>的用例

我遇到过类似的东西:

using arr_t=std::array<std::array<std::array<int,1000>,1000>,1000>;
std::unique_ptr<arr_t> u_ptr;
Run Code Online (Sandbox Code Playgroud)

显然,使用唯一指针来克服stackoverflow问题.有没有使用以前的代码而不仅仅是使用std::vector?有真正的用例std::unique_ptr<std::array<T,N>>吗?

c++ arrays smart-pointers c++11

5
推荐指数
1
解决办法
195
查看次数

为什么不能使用结构化绑定分解lambda表达式的捕获列表

扔烂番茄之前

我知道lambda分解的实际应用目前受到限制,因为无法找到替代失败友好的方法来检查隐藏在分解变量中的lambda捕获数量。这只是一个理论问题,因为我找不到涵盖捕获成员变量访问修饰符的任何标准部分。

int main() {
    int a;
    auto [x] = [a]{};
    static_cast<void>(a);
    static_cast<void>(x);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

标准参考

关于lambda捕获的标准部分很长,因此我可能错过了相关的片段。我注意到的是,重点是对应于捕获的非静态成员必须/未命名。

c++ lambda language-lawyer c++17 structured-bindings

4
推荐指数
1
解决办法
552
查看次数

当短路禁用其评估时,是否允许在常量表达式中读取一个结束指针

考虑示例:

template <char>
struct foo { };

int main() {
    foo<""[0]?""[1]:'\0'>{};
}
Run Code Online (Sandbox Code Playgroud)

代码在[gcc][clang]中编译,但它应该真的吗?我知道表达式""[1]不需要进行评估,因为它是短路的.但是,如果表达式实际上可以作为核心常量表达式,则标准不是很清楚.相关[expr.const]/2,尤其是部分:

如果e满足核心常量表达式的约束,但e的评估将评估具有本文档的[library]至[thread]中指定的未定义行为的操作,则未指定e是否是核心常量表达式.

引起我的怀疑......

c++ short-circuiting language-lawyer constant-expression

4
推荐指数
1
解决办法
160
查看次数