考虑以下代码片段:
int main(){
constexpr int x = -1;
if(x >= 0){
constexpr int y = 1<<x;
}
}
GCC 7(可能还有GCC的其他版本)拒绝编译这篇文章,并说:
error: right operand of shift expression '(1 << -1)' is negative [-fpermissive]
我可以猜到这可能是从哪里来的:constexpr在y上的声明让GCC在编译时评估y,在编译时可能是负的。删除constex
考虑下面的类S,它包含一个函数指针,以及该类的constexpr对象s,该类是用lambda初始化的:
struct S
{
void (*f) ();
};
constexpr S s { []{} };
现在,如果我用S类型的非类型模板参数编写一个模板S,并在s上实例化它,如下所示:
template<S> struct X {};
using x = X<s>;
clang编译代码,但是gcc抱怨说:
error: '<lambda()>::_FUN' is not a valid template argument o
template <bool>
void f() { }
int main ()
{
constexpr long long int num = 5;
f<num & 4>(); // bitwise & is intended
return 0;
}
gcc 9号有个问题:
error: no matching function for call to 'f<(5 & 4)>()'
error: narrowing conversion of '4' from 'lo
我和gcc在非常数容器上使用常量反向迭代器时遇到了问题。嗯,只有某些版本的“gcc”。
#include <vector>
#include <iostream>
using namespace std;
int main() {
const char v0[4] = "abc";
vector<char> v(v0, v0 + 3);
// This block works fine
vector<char>::const_iterator i;
for (i = v.begin()
template <typename Type, Type Func>
struct A
{
};
void func();
A<void(), func> a; // same result with A<void(), &func> a;
此代码使用Clang编译(包括最新的8.0.0),而不是用GCC编译(包括最新的9.1)。
GCC说:error: 'void()' is not a valid type for a template non-type parameter
哪个编译器是正确的,为什么?
更新
我猜GCC是错
在以下内容中,
int i{3};
const int j{3};
extern const int k{3};
template <typename T, T&>
void f() {}
int main()
{
f<int, i>(); // OK
f<int const, j>(); // not valid template argument: 'j' has not external linkage
f<int const, k>(); // OK
}
GCC给出了使
考虑以下代码:
class A {
class B {};
};
template <typename C>
class D : A::B {};
void f() {
D<int> d;
}
D<int>继承自A::B,后者是一个私有嵌套类。我原以为这是一个错误,但GCC接受了这个代码。是GCC的bug,还是我漏掉了什么?
我想证实以下是GCC的错误,而不是我对C++的理解。考虑以下代码:
struct A
{
struct B
{
template< typename U > U as() const { return U(); }
};
B operator[]( int ) const { return B(); }
};
template< typename T >
struct as
{
template< typename U >
static T call( const U& u )
我想到了这门课:
class Point
{
public:
int X, Y;
mutable int Z;
constexpr Point(int x, int y) :X (x), Y(y), Z(0)
{ }
constexpr int GetX() const
{
// Z++; // Wont compile, but following expression is valid!
return X+Z;
}
int GetY() const
{
Z++;
我有一个类似这样的查询: SELECT * FROM mytable
ORDER BY geo_coords <-> ST_MakePoint(<some_lon>, <some_lat>) mytable上的geo_coords列是GEOGRAPHY类型,我在该列上有一个名为myindex的GIST索引。 当我在足够多的记录上运行此查询,以便查询规划器使用索引时,我得到以下错误: org.postgresql.util.PSQLException: ERROR: missing support function 8 for attribute 1 of
clang(主干)给出了以下代码的错误:
consteval void f() {}
int main()
{
f(); // error: call to consteval function 'f' is not a constant expression
// note: subobject of type 'void' is not initialized
}
gcc(主干)则在编译过程中没有出错。
我觉得这可能是一个clang错误,因为gcc和clang都接受这个代码:
consteval int g() { ret
我发现使用常量引用参数调用常量error静态方法会导致对于clang和icc都会出现错误"expression is not a integral constant“,但对于gcc或msvc ()则不会:
struct S
{
static constexpr bool ok() { return true; }
};
constexpr void ff(const S &s) // OK for everyone if not a ref
{
static_assert(s.ok(), "oops!"); // ERROR clang/icc,
下面的代码用gcc和MSVC编译,但是使用clang-3.5和当前主干测试的clang我失败了。
template <typename T>
constexpr auto wrong = false;
template <typename T>
constexpr auto foo(const T t) -> int
{
static_assert(wrong<T>, "");
return {};
}
using F = decltype(foo(1));
int main() {}
clang实例化函数体并在stati
问一个朋友:
为什么下面代码中的std::forward会将参数c强制转换为右值?
template <typename T>
void f (T& c) {
using value_type = typename std::remove_reference_t<T>;
std::vector<value_type> v;
// debugger reveals: push_back( T&& value ) is called here
v.push_back(std::forward<