Consider the 2 following overloads
template<typename T>
bool test() {
return true;
}
template<template<typename ...> class T>
bool test() {
return false;
}
Run Code Online (Sandbox Code Playgroud)
The 1st one works for regular classes, while the 2nd one works for templates that are not instantiated. For instance:
std::cout<<test<int>()<<std::endl; <-- this yields 1
std::cout<<test<std::list>()<<std::endl; <--this yields 0
Run Code Online (Sandbox Code Playgroud)
Now consider the following template function:
template<typename U>
bool templfun(){
struct A{
bool f(){
return test<A>(); // <-- this gives an error
}
};
return test<A>(); // <-- …Run Code Online (Sandbox Code Playgroud) 以下代码不能在 clang 中编译(在 GCC 中可以):
struct A{
int a;
};
auto test(){
constexpr A x{10};
return []{
return x; // <-- here x is A: clang doesn't compile
}();
}
Run Code Online (Sandbox Code Playgroud)
Clang 的错误是变量 'x' 不能在没有指定捕获默认值的 lambda 中隐式捕获,但我认为 constexpr 变量总是被捕获。
如果 x 是 int,则代码编译:
auto test(){
constexpr int x{10};
return []{
return x; // <-- here x is int: clang is ok
}();
}
Run Code Online (Sandbox Code Playgroud)
有趣的是,以下代码也可以编译:
auto test(){
constexpr A x{10};
return []{
return x.a;
}();
}
Run Code Online (Sandbox Code Playgroud)
叮当正确吗?如果是这样,理由是什么?我正在使用 -std=c++17
- 编辑 …
我有一个Renderscript,它处理输出中给出的分配图像.我想在我的OpenGL程序中将此Allocation用作纹理,但我不知道如何从分配中获取纹理ID.
另一方面,我知道我可以使用图形Renderscript,但由于它已被弃用,我想必须有其他方法来实现相同的结果.
此代码将无法编译:
struct A{
static constexpr auto f(){
return []{};
}
static constexpr auto x = f();
};
Run Code Online (Sandbox Code Playgroud)
错误:在扣除“auto”之前使用“static constexpr auto A::f()”
我知道有多个与此类似的问题,但我真的不明白在这种特定情况下是什么阻止了编译器完成其工作。另请注意,在类定义之外的相同代码可以完美运行。
我有以下 c++14 代码:
template<typename T>
struct Test{
static constexpr auto something{T::foo()};
};
Run Code Online (Sandbox Code Playgroud)
这完全没问题,前提是它T::foo()也是一个constexpr。
现在我已经使用了somethingODR,所以我需要提供一个命名空间声明。我应该使用什么语法?
template<typename T>
constexpr auto Test<T>::something;
Run Code Online (Sandbox Code Playgroud)
不起作用。谢谢!
在c ++ 14中,我有以下类型:
std::tuple<int[2], int>;
Run Code Online (Sandbox Code Playgroud)
我该如何正确初始化它?这个
std::tuple<int[2], int> a {{2,2},3};
Run Code Online (Sandbox Code Playgroud)
给我这个错误:
/ usr/include/c ++/5/tuple:108:25:error:用作初始化程序的数组
这个:
std::tuple<std::array<int,2>, int> a {{2,2},3};
Run Code Online (Sandbox Code Playgroud)
有效,但我希望能够使用标准的C风格数组