小编Hrv*_*rić的帖子

C++ SFINAE 没有失败

代码:

#include <iostream>


using std::nullptr_t;

template<typename... T>
using nullptr_vt = nullptr_t;

struct not_addable{};


template<
  typename T, 
  nullptr_vt<decltype(std::declval<T>() + std::declval<T>())> TSfinae = nullptr>
bool test_addable(int)
{ return true; }

template<typename>
bool test_addable(...)
{ return false; }


int main()
{
  std::cout << std::boolalpha;

  std::cout << test_addable<int>(0) << std::endl;
  std::cout << test_addable<not_addable>(0) << std::endl;

  // Gives error ("invalid operands to binary expression"):
  // nullptr_vt<decltype(std::declval<not_addable>() + std::declval<not_addable>())> a{};
}
Run Code Online (Sandbox Code Playgroud)

我以为这会打印:

true
false
Run Code Online (Sandbox Code Playgroud)

,但事实并非如此。它打印:

true 
true
Run Code Online (Sandbox Code Playgroud)

. 至少在https://repl.it/@Hrle/sfinaetemplatesuccess 上

我认为nullptr_vt<decltype(std::declval<T>() + std::declval<T>())> …

c++ templates sfinae

6
推荐指数
1
解决办法
104
查看次数

C++条件三元运算符

第11,12,15和16行正在获得错误:"int类型的无效操作数和const char [2]到二元运算符<<"(我删除了"`",因此它不会显示代码格式).

#include<iostream>

using namespace std;

int main(){

int md,dg,dd,mg,m,d;
cin >> md >> dg >> dd >> mg;

if (dd+dg==md+mg){
   cout << (mg>dg) ? 0 : 1 << " ";
   cout << (dg>mg) ? 0 : 1 << endl;
}
else{
      cout << (mg+md>dd+dg) ? 0 : (dd+dg-mg-md) << " ";
      cout << (dg+dd>md+mg) ? 0 : (md+mg-dg-dd) << endl;
}

system("pause");
}
Run Code Online (Sandbox Code Playgroud)

c++ conditional ternary operator-keyword

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

标签 统计

c++ ×2

conditional ×1

operator-keyword ×1

sfinae ×1

templates ×1

ternary ×1