鉴于以下程序
#include <iostream>
template<class T> struct id { using type = T; };
template<class T1, class T2>
int func(T1, T2) { return 0; }
template<class T1, class T2>
int func(typename id<T1>::type, typename id<T2>::type) { return 1; }
int main()
{
std::cout << func<int, int>(0, 0) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
GCC 和 Clang 都打印1了这个程序。这个程序是否保证1按标准打印?
我试图在这里找到答案,但无法破译。看起来函数模板可能是等效的,因此会破坏 ODR,但我不确定。
是否将第二个函数模板更改为
template<class T>
using id_type = typename id<T>::type;
template<class T1, class T2>
int func(id_type<T1>, id_type<T2>) { return 1; …Run Code Online (Sandbox Code Playgroud) 为什么新std::sentinel_for概念要求哨兵类型是default_initializable(via semiregular)?这是否排除了一大类有用的哨兵类型,其中默认构造没有任何意义?
例子:
//Iterate over a string until given character or '\0' is found
class char_sentinel
{
public:
char_sentinel(char end) :
end_character(end)
{ }
friend bool operator==(const char* lhs, char_sentinel rhs)
{
return (*lhs == '\0') || (*lhs == rhs.end_character);
}
friend bool operator!=(const char* lhs, char_sentinel rhs) { ... }
friend bool operator==(char_sentinel lhs, const char* rhs) { ... }
friend bool operator!=(char_sentinel lhs, const char* rhs) { ... }
private:
char end_character;
}; …Run Code Online (Sandbox Code Playgroud) 我正在使用这个std::tuple课程,发现我会说的是一些意想不到的行为.
考虑一下代码:
#include <iostream>
#include <tuple>
int i = 20;
std::tuple<int&, int> f() {
return std::tuple<int&, int>(i, 0);
}
int main() {
const std::tuple<int, int>& t = f();
int j = ++i;
std::cout << std::get<0>(t) << "\n";
}
Run Code Online (Sandbox Code Playgroud)
这似乎20在所有主要编译器上编译和打印.这两种类型的标准是否符合或未定义的行为是不同的?我知道可以通过分配它来延长临时的生命周期const T&,但据我所知,std::tuple<int&, int>它的类型不同std::tuple<int, int>.