#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++ 17标准27.2.1.8说:
迭代器j被称为可以从迭代器i到达,当且仅当存在表达式++ i的有限序列的应用程序时才使i == j.
也就是说,任何符合标准的迭代器类型都必须提供operator ==.
但是,我发现没有任何关于operator !=迭代器类型的要求.
是否operator !=必须为给定的迭代器类型提供C++标准要求?
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,哪个字符在性能方面是首选?
#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> ?
#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}\nRun Code Online (Sandbox Code Playgroud)\nC++ 是否保证 lambda 未命名类始终具有已operator bool()定义的?
考虑以下两个函数:
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
#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?
我认为以下代码是邪恶的,但它可以在没有任何警告的情况下编译.
int f(int n)
{
return n + 1;
}
int n = 0;
n = f(n++) + f(++n);
Run Code Online (Sandbox Code Playgroud)
我只是想知道为什么神圣标准不会弃用这些运算符?
我猜可能有两个原因:
一个可能是向后兼容;
另一种可能是在某些情况下,这些运算符非常有用.
如果后者是真的,你能给我一些例子吗?谢谢.
c++ ×10
standards ×5
type-traits ×4
c++20 ×3
c++-concepts ×2
iterator ×2
performance ×2
string ×2
algorithm ×1
c ×1
c++14 ×1
c++17 ×1
clang ×1
coding-style ×1
compiler-bug ×1
constants ×1
destructor ×1
lambda ×1
optimization ×1
overloading ×1
semantics ×1
sfinae ×1
stl ×1
vector ×1