小编Mes*_*kon的帖子

函数模板重载解析,依赖和非依赖参数

鉴于以下程序

#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)

c++ templates language-lawyer c++17

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

为什么 std::sentinel_for 概念需要默认的可构造性?

为什么新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)

c++ std language-lawyer c++-concepts c++20

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

通过将std :: tuple <int,int>赋值给const std :: tuple <int,int>&来延长它的生命周期

我正在使用这个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>.

c++ language-lawyer

5
推荐指数
2
解决办法
77
查看次数

标签 统计

c++ ×3

language-lawyer ×3

c++-concepts ×1

c++17 ×1

c++20 ×1

std ×1

templates ×1