小编dav*_*ave的帖子

Is this a bug in std::gcd?

I've come across this behavior of std::gcd that I found unexpected:

#include <iostream>
#include <numeric>

int main()
{
    int      a = -120;
    unsigned b =  10;

    //both a and b are representable in type C
    using C = std::common_type<decltype(a), decltype(b)>::type;
    C ca = std::abs(a);
    C cb = b;
    std::cout << a << ' ' << ca << '\n';
    std::cout << b << ' ' << cb << '\n';

    //first one should equal second one, but doesn't
    std::cout << std::gcd(a, b) …
Run Code Online (Sandbox Code Playgroud)

c++ unsigned libstdc++ language-lawyer c++17

17
推荐指数
1
解决办法
419
查看次数

模板(.tpp)文件包含警卫

在编写模板化类时,我喜欢将实现移动到另一个文件(myclass.tpp)中并将其包含在主标题(myclass.hpp)的底部.

我的问题是:我是否需要在.tpp文件中包含保护,或者将它们放在.hpp文件中是否足够?

示例代码:

myclass.hpp

#ifndef MYCLASS_HPP
#define MYCLASS_HPP

template<typename T>
class MyClass
{
public:
    T foo(T obj);
};

//include template implemetation
#include "myclass.tpp"

#endif
Run Code Online (Sandbox Code Playgroud)

myclass.tpp

#ifndef MYCLASS_TPP //needed?
#define MYCLASS_TPP //needed?

template<typename T>
T MyClass<T>::foo(T obj)
{
    return obj;
}

#endif //needed?
Run Code Online (Sandbox Code Playgroud)

c++ templates include-guards

13
推荐指数
1
解决办法
875
查看次数

在Qt5代码中禁用旧的(Qt4样式)Signal-Slot语法

有什么方法可以防止或不鼓励在仅以Qt5编写的项目中使用来自Qt4 旧Signal-Slot语法

在我们当前的项目中,没有出现旧语法,我也没有任何理由支持它们。因此,我们想完全禁用它以防止意外使用。这是可能的,例如通过在中定义某些符号。pro文件?

我知道使用自定义Linter规则应该可以实现,但不幸的是我们还没有集中化规则。

//old way. should throw a compiler error or warning
connect(sender, SIGNAL(sig), receiver, SLOT(slt));

//new way
connect(sender, &Send::sig, receiver, &Rec::slt);
Run Code Online (Sandbox Code Playgroud)

c++ qt signals-slots qt5 c++17

10
推荐指数
2
解决办法
134
查看次数

从 std::ranges 算法获取投影值

我正在使用std::ranges(maxmax_element) 中的算法进行投影。结果有可能也是预测值吗?目前我必须对返回值再次调用投影函数。

示例:这里我想要最长字符串的大小,但算法仅返回字符串或迭代器。

int main()
{
    const std::vector<std::string> vec = {
        "foo",
        "hello",
        "this is a long string",
        "bar"
    };

    //r1 is a string. r2 is an iterator
    const auto r1 = std::ranges::max(vec, {}, &std::string::size);
    const auto r2 = std::ranges::max_element(vec, {}, &std::string::size);
    
    //I have to call size() again
    std::cout << r1 << '\n' << *r2 << '\n';
    std::cout << r1.size() << '\n' << r2->size() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

编译器资源管理器

c++ c++20 std-ranges

4
推荐指数
2
解决办法
1150
查看次数