小编eXX*_*XX2的帖子

SFINAE 检测静态方法

我正在尝试实现一种机制来检测提供的类是否包含某些静态方法。这是非常简单的代码,但我无法理解为什么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)

c++ templates sfinae template-specialization c++11

5
推荐指数
1
解决办法
976
查看次数

简单的递归排序算法很难理解

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

c++ sorting algorithm

3
推荐指数
1
解决办法
156
查看次数

自动变量以存储指向std :: max的函数指针

我正在尝试将函数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++ templates auto template-argument-deduction

3
推荐指数
1
解决办法
350
查看次数

可互换使用的类型和非类型模板参数

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

c++ templates

2
推荐指数
1
解决办法
45
查看次数

未实例化的模板的 C++ 标准要求

所以我尝试编译下面的代码但它失败了(正如预期的那样):

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)

c++ templates template-instantiation

2
推荐指数
1
解决办法
122
查看次数

钩子和线程,鼠标阻塞

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

c++ winapi multithreading

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

JFrame对象的垃圾收集器

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的对象?

java swing garbage-collection jframe

1
推荐指数
2
解决办法
873
查看次数

AppDomain.CurrentDomain.ProcessExit 不执行 Console.WriteLine

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 个序列化不会发生这种行为。

c# appdomain

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

为什么异常类型会影响将要编译的内容?

我有两个版本的代码.在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)

java exception-handling

0
推荐指数
1
解决办法
110
查看次数

表达式((sizetype)(((ssizetype)length)+ -1))+ 1

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呢?

c++ gcc

0
推荐指数
1
解决办法
128
查看次数