小编Rus*_*lan的帖子

为什么不能在没有显式&on函数名的情况下将函数指针与模板函数进行比较?

请考虑以下代码:

void func(int) {}
template<typename T> void templatedFunc(T) {}
int main()
{
    void (*p)(int) = func;

    bool test1 = p==func;
    //bool test2 = p==templatedFunc<int>; // compilation error
    bool test3 = p==&templatedFunc<int>; // but this works
}
Run Code Online (Sandbox Code Playgroud)

如果取消注释该test2行并尝试使用g ++编译代码,您将收到以下错误:

test.cpp: In function ‘int main()’:
test.cpp:8:21: error: assuming cast to type ‘void (*)(int)’ from overloaded function [-fpermissive]
     bool test2 = p==templatedFunc<int>; // compilation error
                     ^~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

我在g ++ 5.3.0和6.2.0上得到了这个结果.与此同时,使用clang ++ 3.6.0进行编译会成功,而不会发出警告.

根据此处的标准,哪个编译器是正确的 - g ++,它给出了错误或clang ++,哪个没有?

如果g ++是正确的,那么为什么有关于需要显式地址运算符的正常函数与模板化函数存在这种不对称性?

c++ templates function-pointers

12
推荐指数
1
解决办法
187
查看次数

虽然是NaN,为什么打印这个值?

以下代码假定我们在x86兼容系统上并long double映射到x87 FPU的80位格式.

#include <cmath>
#include <array>
#include <cstring>
#include <iomanip>
#include <iostream>

int main()
{
    std::array<uint8_t,10> data1{0x52,0x23,0x6f,0x24,0x8f,0xac,0xd1,0x43,0x30,0x02};
    std::array<uint8_t,10> data2{0x52,0x23,0x6f,0x24,0x8f,0xac,0xd1,0xc3,0x30,0x02};
    std::array<uint8_t,10> data3{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x30,0x02};
    long double value1, value2, value3;
    static_assert(sizeof value1 >= 10,"Expected float80");
    std::memcpy(&value1, data1.data(),sizeof value1);
    std::memcpy(&value2, data2.data(),sizeof value2);
    std::memcpy(&value3, data3.data(),sizeof value3);
    std::cout << "isnan(value1): " << std::boolalpha << std::isnan(value1) << "\n";
    std::cout << "isnan(value2): " << std::boolalpha << std::isnan(value2) << "\n";
    std::cout << "isnan(value3): " << std::boolalpha << std::isnan(value3) << "\n";
    std::cout << "value1: " << std::setprecision(20) << …
Run Code Online (Sandbox Code Playgroud)

c++ floating-point nan

11
推荐指数
1
解决办法
465
查看次数

这种向下倾斜是不确定的?

考虑这个例子,其中基类有一些数据成员,而派生类只提供了一个额外的方法:

struct TestBase
{
    int x;
    TestBase() : x(5) {}
};

struct TestDerived : public TestBase
{
    void myMethod()
    {
        x=8;
    }
};

int main()
{
    TestBase b;
    TestDerived& d=static_cast<TestDerived&>(b);
    d.myMethod();
}
Run Code Online (Sandbox Code Playgroud)

这是向下错误的类型,所以AFAIU它有未定义的行为.但是这样的情况可能会有一些例外吗,派生类的布局与基类的布局相同?

c++ downcast undefined-behavior

11
推荐指数
1
解决办法
372
查看次数

为什么用g ++编译该代码会花费这么长时间?

考虑以下代码:

template<int i> class A
{
    typedef A<i-1> B;
    B x, y;
};
template<> class A<0> { char m; };
int main()
{
    A<LEVEL> a;
}

Run Code Online (Sandbox Code Playgroud)

通过以下Bash命令(使用g ++ 8.3.0)对g ++编译进行基准测试时

template<int i> class A
{
    typedef A<i-1> B;
    B x, y;
};
template<> class A<0> { char m; };
int main()
{
    A<LEVEL> a;
}

Run Code Online (Sandbox Code Playgroud)

我得到以下输出:

1,0.03
2,0.03
3,0.04
4,0.04
5,0.04
6,0.04
7,0.04
8,0.04
9,0.03
10,0.04
11,0.02
12,0.04
13,0.02
14,0.03
15,0.04
16,0.05
17,0.05
18,0.08
19,0.11
20,0.20
21,0.35
22,0.67
23,1.30
24,2.52 …
Run Code Online (Sandbox Code Playgroud)

c++ gcc templates compilation

11
推荐指数
1
解决办法
142
查看次数

未定义的行为会影响static_assert吗?

考虑以下代码:

SomeType x=getX();
for(auto mask = 1u<<(CHAR_BIT*sizeof x - 1);/*...*/;/*...*/)
{
    static_assert(sizeof mask>=sizeof x, "Type of numeric parameter is too long");
    /*...*/
}
Run Code Online (Sandbox Code Playgroud)

在这里,mask将具有type unsigned。假设SomeTypelong long。然后,mask由于移动过多,初始化会具有不确定的行为。但是OTOH中有一个static_assert,用于检查在运行时不会发生未定义的行为(因为代码将无法编译)。

但是,由于UB可能导致时间悖论和其他意外事件,因此我不确定static_assert在这种情况下是否可以保证它确实有效。有什么理由可以确定吗?还是应该static_assert在初始化之前重做此代码mask

c++ static-assert undefined-behavior

10
推荐指数
1
解决办法
145
查看次数

模板参数推导:哪个编译器就在这里?

请考虑以下代码:

template<int N>
class Vector
{
};

#include <array>

template<int N>
void doWork(const Vector<N>&, const std::array<int,N>&)
{
}

int main()
{
    std::array<int,3> arr;
    Vector<3> vec;
    doWork(vec,arr);
}
Run Code Online (Sandbox Code Playgroud)

这里Vector表示一个在第三方库中定义的类,并且std::array已知将其元素计为std::size_t.

我试过用clang-3.6和g ++ - 5.1编译它.Clang没有任何投诉,而g ++给出了以下错误:

test.cpp: In function ‘int main()’:
test.cpp:17:19: error: no matching function for call to ‘doWork(Vector<3>&, std::array<int, 3ul>&)’
     doWork(vec,arr);
                   ^
test.cpp:9:6: note: candidate: template<int N> void doWork(const Vector<N>&, const std::array<int, N>&)
 void doWork(const Vector<N>&, const std::array<int,N>&)
      ^
test.cpp:9:6: note:   template argument deduction/substitution failed: …
Run Code Online (Sandbox Code Playgroud)

c++ g++ clang language-lawyer c++11

9
推荐指数
1
解决办法
385
查看次数

绑定引用实际上是否评估操作数?

考虑以下代码:

int& x=*new int;
Run Code Online (Sandbox Code Playgroud)

赋值的RHS是否实际取消引用新创建的指针,由于读取未初始化的变量而导致UB?或者这可以合法地用于以后分配像x=5;

c++ initialization reference undefined-behavior

9
推荐指数
1
解决办法
121
查看次数

GLFW的首字母缩写词代表什么?

GLFW的首字母缩写词代表什么?http://www.glfw.org/是主要网站,但我找不到任何线索.在SO上它有一个标签,但在描述中没有解释首字母缩略词.

glfw

8
推荐指数
1
解决办法
3273
查看次数

为什么模板类中的函数声明无效?

请考虑以下代码:

template<int X, int Y>
struct S
{
    typedef int func(int,float) const;
};

template<int X>
struct D : public S<X,6>
{
    typename S<X,6>::func func;
};
template<int X>
int D<X>::func(int,float) const
{
    return 1;
}
//----------------
struct W : public S<7,8>
{
    S<7,8>::func func;
};
int W::func(int,float) const
{
    return 2;
}

#include <iostream>
int main()
{
    W w;
    std::cout << w.func(1,4.3) << "\n";
    D<3> d;
    std::cout << d.func(1,4.3) << "\n";
}
Run Code Online (Sandbox Code Playgroud)

如果我注释掉的代码声明类DD::func(),以及在相应的行main(),代码编译正常,我看到2在输出,符合市场预期. …

c++ methods templates types

8
推荐指数
1
解决办法
937
查看次数

这个代码是否定义明确,无论复制省略?

考虑以下代码:

#include <iostream>

struct Test
{
    int x;
    int y;
};

Test func(const Test& in)
{
    Test out;
    out.x=in.y;
    out.y=in.x;
    return out;
}

int main()
{
    Test test{1,2};
    std::cout << "x: " << test.x << ", y: " << test.y << "\n";
    test=func(test);
    std::cout << "x: " << test.x << ", y: " << test.y << "\n";
}
Run Code Online (Sandbox Code Playgroud)

人们会期望这样的输出:

x: 1, y: 2
x: 2, y: 1
Run Code Online (Sandbox Code Playgroud)

这确实是我得到的.但由于复制省略,可能out在内存中的同一位置in并导致最后一行输出x: 2, y: 2

我试着用gcc和铿锵既编译 …

c++ undefined-behavior copy-elision

8
推荐指数
1
解决办法
402
查看次数