我正在尝试实现一种机制来检测提供的类是否包含某些静态方法。这是非常简单的代码,但我无法理解为什么decltype()不能按EnableIfHasFooMethod类专业化的预期工作:
#include <iostream>
struct A {
static int Foo() { return 0; }
};
template <class T, class = void>
struct EnableIfHasFooMethod {};
template <class T>
struct EnableIfHasFooMethod<T, decltype(T::Foo)> {
typedef void type;
};
template <class T, class = void>
struct HasFooMethod {
static const bool value = false;
};
template <class T>
struct HasFooMethod<T, typename EnableIfHasFooMethod<T>::type> {
static const bool value = true;
};
int main() {
std::cout << HasFooMethod<A>::value << std::endl;
return 0;
} …Run Code Online (Sandbox Code Playgroud) #include <iostream>
#include <algorithm>
using namespace std;
const int N = 100000;
void sort(int* a, int lo, int hi)
{
int i = lo;
if (lo >= hi)
return;
for (int j = hi, mode = 1; i < j; mode > 0 ? j-- : i++)
if (a[i] > a[j])
{
swap(a[i], a[j]);
mode = -mode;
}
sort(a, lo, i - 1);
sort(a, i + 1, hi);
}
bool check(int* a)
{
for (int i = 1; i < …Run Code Online (Sandbox Code Playgroud) 我正在尝试将函数std::max作为模板参数传递给模板化函数,但是由于某些原因,编译器会打印出无法推断出函数类型的错误。一个简单的例子也重现了同样的问题。它具有自己的max2功能,但不适用于STL std::max:
#include <algorithm>
template <class T>
T max2(const T& a, const T& b) { return std::max(a, b); }
int main() {
#if 1
auto f = max2<float>;
#else
// error: unable to deduce ‘auto’ from ‘max<float>’
auto f = std::max<float>;
#endif
float max_val = f(1.0f, 2.0f);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在C++中是否可以声明某个类,以便允许它传递整数值或类型作为模板参数?
像这样的东西:
#include <iostream>
using namespace std;
template <auto I>
struct Foo {};
int main()
{
Foo<int> foo1;
Foo<1> foo2;
return 0;
}
Run Code Online (Sandbox Code Playgroud) 所以我尝试编译下面的代码但它失败了(正如预期的那样):
1.cpp: In function ‘int foo()’:
1.cpp:3:5: error: ‘some’ was not declared in this scope
some ill-formed code
^
Run Code Online (Sandbox Code Playgroud)
但是如果我删除这一行,编译器会编译它而不会出现任何错误(也是预期的,因为T类型是否有random_name()方法是未知的)。
似乎未使用(未实例化)的模板的诊断在某种程度上是实现定义的。但也许标准对这种情况有一些要求。例如,编译下面的代码是否符合标准而没有任何错误?
我试图在网站上搜索答案,但找不到任何相关问题。
template <class T>
int foo() {
some ill-formed code
return T::random_name();
}
template <>
int foo<int>() { return 0; }
int main() {
return foo<int>();
}
Run Code Online (Sandbox Code Playgroud) #include <windows.h>
#include <process.h>
HWND MainHwnd;
HHOOK MouseHook;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg,WPARAM wParam,LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,PSTR szCmdLine,int iCmdShow)
{
static wchar_t szAppName[]=L"hooks";
HWND hwnd;
MSG msg;
WNDCLASSEX wndclass;
wndclass.cbSize=sizeof(wndclass);
wndclass.style=CS_HREDRAW|CS_VREDRAW;
wndclass.lpfnWndProc=WndProc;
wndclass.cbClsExtra=0;
wndclass.cbWndExtra=0;
wndclass.hInstance=hInstance;
wndclass.hIcon=LoadIcon(NULL,IDI_APPLICATION);
wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName=NULL;
wndclass.lpszClassName=szAppName;
wndclass.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
RegisterClassEx(&wndclass);
MainHwnd=hwnd=CreateWindow(szAppName,L"hooks",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
ShowWindow(hwnd,SW_MAXIMIZE);
UpdateWindow(hwnd);
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK LowLevelMouseProc(int nCode,WPARAM wParam,LPARAM lParam)
{
if (nCode==HC_ACTION)
((LPMSLLHOOKSTRUCT)lParam)->flags=0;
return CallNextHookEx(NULL,nCode,wParam,lParam);
}
void thread(void *param)
{
for (int i=0;i<3;i++)
{
UnhookWindowsHookEx(MouseHook);
MouseHook=SetWindowsHookEx(WH_MOUSE_LL,reinterpret_cast<HOOKPROC>(LowLevelMouseProc),(HINSTANCE)GetWindowLong(MainHwnd,GWL_HINSTANCE),NULL); …Run Code Online (Sandbox Code Playgroud) import javax.swing.*;
public class Main
{
public Main()
{
JFrame jf = new JFrame("Demo");
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setSize(100, 100);
jf.setVisible(true);
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
new Main();
}
});
Runtime.getRuntime().gc();
}
}
Run Code Online (Sandbox Code Playgroud)
我呼吁Runtime.getRuntime().gc();显式垃圾收集器调用.但窗口不会从屏幕上消失,为什么垃圾收集器不回收JFrame的对象?
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace empty
{
class Program
{
static Program()
{
AppDomain.CurrentDomain.ProcessExit += ExitHandler;
}
static void Main(string[] args)
{
}
static void ExitHandler(object o, EventArgs args)
{
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
using (FileStream fs = new FileStream("file.bin", FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs, new double[30000000]);
}
Console.WriteLine("end");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我希望得到输出:“结束”但什么也得不到。我究竟做错了什么?
我故意使用 2 个序列化,因为 1 个序列化不会发生这种行为。
我有两个版本的代码.在Method()中抛出的第一个版本类型的异常 - NullPointerException在第二个版本中 - Exception.但是第一个版本将编译但第二个版本将无法编译.为什么会这样?
public class Demo
{
static void Method()
{
try
{
throw new NullPointerException("error");
}
catch(Exception ex)
{
throw ex;
}
}
public static void main(String argv[])
{
try
{
Method();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二个版本.
public class Demo
{
static void Method()
{
try
{
throw new Exception("error");
}
catch(Exception ex)
{
throw ex;
}
}
public static void main(String argv[])
{
try
{
Method();
}
catch(Exception ex)
{ …Run Code Online (Sandbox Code Playgroud) template <int I>
struct A {};
int main()
{
int length = 1;
int ar[length];
A<sizeof(ar)> a;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到错误:(unsigned int)((((unsigned int)(((int)length) + -0x000000001)) + 1u) * 4u)不是类型'int'的有效模板参数,因为它是非常量表达式
这种表达的意义是什么?为什么不能这样length * 4u呢?