小编xml*_*lmx的帖子

为什么SFINAE在这种情况下不起作用?

#include <iostream>
#include <type_traits>

template<typename T>
struct A
{
    using m = std::remove_pointer_t<T>&;
};

template
<
    typename T,
    typename = std::void_t<>
>
struct Test
{
    enum { value = 0 };
};

template<typename T>
struct Test<T, typename A<T>::m>
{
    enum { value = 1 };
};

int main()
{
    std::cout << Test<void*&>::value; // ok, output 0
    std::cout << Test<void*>::value; // error : cannot form a reference to 'void'
}
Run Code Online (Sandbox Code Playgroud)

第一种情况输出0,表示选择了主模板.所以,我认为第二种情况也应该选择主要模板而不是专用模板; 那么,应该没有错误.

预计Test<void*&>没关系; 让我感到惊讶的是Test<void*>应该不行!

为什么 …

c++ overloading sfinae type-traits c++17

0
推荐指数
1
解决办法
77
查看次数

C++标准是否需要operator!=必须为给定的迭代器类型提供?

C++ 17标准27.2.1.8说:

迭代器j被称为可以从迭代器i到达,当且仅当存在表达式++ i的有限序列的应用程序时才使i == j.

也就是说,任何符合标准的迭代器类型都必须提供operator ==.

但是,我发现没有任何关于operator !=迭代器类型的要求.

是否operator !=必须为给定的迭代器类型提供C++标准要求?

c++ standards iterator type-traits semantics

0
推荐指数
1
解决办法
63
查看次数

哪个更好检查一个字符是否存在于std :: string中?find或find_first_of?

std::string 有两个不同的成员函数做同样的事情:

size_type find( CharT ch, size_type pos = 0 ) const noexcept;
size_type find_first_of( CharT ch, size_type pos = 0 ) const noexcept;
Run Code Online (Sandbox Code Playgroud)

如果我想检查a中是否存在某个字符std::string,哪个字符在性能方面是首选?

c++ string algorithm performance stl

0
推荐指数
1
解决办法
117
查看次数

为什么 std::vector::iterator 不是连续迭代器?

按照cppref页面std::vector

iterator 传统随机访问迭代器

同样来自另一个cppref 页面

以下标准库类型是LegacyContiguousIterators

vector::iterator 对于 bool 以外的 value_type。

哪个是正确的?

c++ iterator type-traits c++-concepts c++20

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

当 std::is_trivial_v&lt;T&gt; 为真时 T 可以有析构函数吗?

#include <type_traits>

struct A
{
    ~A() {}
};

int main()
{
    static_assert(std::is_trivial_v<A>); // error   
}
Run Code Online (Sandbox Code Playgroud)

它似乎很明显,std::is_trivial_v<A>将是false如果A有一个析构函数。

但是,从 的cppref 页面std::is_trivial,没有什么要求A一定不能有析构函数。

什么时候可以 T 有析构函数 std::is_trivial_v<T>

c++ standards destructor type-traits c++-concepts

0
推荐指数
1
解决办法
112
查看次数

C++ 是否保证 lambda 未命名类始终定义“operator bool()”?

#include <type_traits>\n\nint main()\n{\n    auto f = [] {};\n    static_assert(std::is_same_v<decltype(!f), bool>); // ok\n\n    f.operator bool(); // error: \xe2\x80\x98struct main()::<lambda()>\xe2\x80\x99 \n                       // has no member named \xe2\x80\x98operator bool\xe2\x80\x99\n}\n
Run Code Online (Sandbox Code Playgroud)\n

C++ 是否保证 lambda 未命名类始终具有已operator bool()定义的?

\n

c++ lambda standards operator-overloading c++14

0
推荐指数
1
解决办法
58
查看次数

std::string::resize(smaller_than_capacity) 是否保证现有迭代器仍然有效?

根据cpprefstd::vector::resize明确保证:

当调整到较小的大小时,向量容量永远不会减少,因为这会使所有迭代器无效,而不仅仅是那些会被等效pop_back()调用序列无效的迭代器。

但是,我无法找到从任何类似保证文档std::string::resize

是否 std::string::resize(smaller_than_capacity) 保证现有的迭代器仍然有效?

c++ string standards vector c++20

0
推荐指数
1
解决办法
84
查看次数

f(int const) 是否比 f(int) 更适合编译器优化?

考虑以下两个函数:

void f(int n);

void f(int const n);
Run Code Online (Sandbox Code Playgroud)

从编译器优化的角度来看,后者似乎比前者更好。

然而,从调用者的角度来看,比 .int const n拥有更多的无用信息更多的精神负担int n

哪个是更好的做法?

c++ optimization performance constants compiler-optimization

0
推荐指数
1
解决办法
127
查看次数

为什么最新的 clang 没有定义功能测试宏 __cpp_coroutines?

#include <iostream>

int main() {
#if __has_include(<coroutine>)
    std::cout << "__has_include(<coroutine>)" << std::endl;
#endif

#if defined(__cpp_impl_coroutine)
    std::cout << "__cpp_impl_coroutine is defined." << std::endl;
#endif

#if defined(__cpp_coroutines)
    std::cout << "__cpp_coroutines is defined." << std::endl;
#else
    std::cout << "__cpp_coroutines IS NOT defined!" << std::endl;
#endif
}
Run Code Online (Sandbox Code Playgroud)

我的编译器是 clang-18.1.0。

使用 构建代码clang++ -std=c++20 -stdlib=libc++ ./main.cpp,输出为:

__has_include(<coroutine>)
__cpp_impl_coroutine is defined.
__cpp_coroutines IS NOT defined!
Run Code Online (Sandbox Code Playgroud)

为什么最新的 clang 没有定义功能测试宏 __cpp_coroutines?

c++ clang compiler-bug c++20 c++-coroutine

0
推荐指数
1
解决办法
85
查看次数

为什么C++标准不会弃用增量/减量运算符?

我认为以下代码是邪恶的,但它可以在没有任何警告的情况下编译.

int f(int n)
{
    return n + 1;
}

int n = 0;
n = f(n++) + f(++n);
Run Code Online (Sandbox Code Playgroud)

我只是想知道为什么神圣标准不会弃用这些运算符?

我猜可能有两个原因:

一个可能是向后兼容;

另一种可能是在某些情况下,这些运算符非常有用.

如果后者是真的,你能给我一些例子吗?谢谢.

c c++ standards coding-style unary-operator

-1
推荐指数
1
解决办法
386
查看次数