这是一个非常简单的模板问题(我认为对C++专家来说很简单),涉及制作通用数学函数.我有一个简单的Epsilon函数,如下所示:
template<class T>
static T Epsilon()
{
return std::numeric_limits<T>::Min();
}
Run Code Online (Sandbox Code Playgroud)
我想将它分配给一些变量,如下所示:
float epsilon = Math::Epsilon();
Run Code Online (Sandbox Code Playgroud)
,唉,我收到编译错误:
错误C2783:'T Math :: Epsilon(void)':无法推断'T'的模板参数
我可以像这样分配它,没有错误:
float epsilon = Math::Epsilon<float>();
Run Code Online (Sandbox Code Playgroud)
我以为模板引擎能够看到我的T是"浮动",但显然它不能.我在这里没有理解什么?
我的理解是,默认情况下,Dictionary中的键检查是区分大小写的,但至少在我的软件中看来并非如此.为了获得TryGetValue和Contains的区分大小写的密钥检查,我必须按如下方式构造我的Dictionary:
Dictionary<string, string> a = new Dictionary<string,string>(StringComparer.Ordinal);
Run Code Online (Sandbox Code Playgroud)
那我错了吗?字典是否默认情况下不区分大小写?
这可能是今天提出的最愚蠢的问题,但无论如何我都要按下:
以下带有重载运算符的派生类,其重载运算符+=的基类[]给出以下编译器错误:
不允许使用空属性块
我当然会写v[0]和v[1]操作员+=,但是我很好奇它是否会编译,如果没有,为什么不.
什么是属性块?为什么编译器不解析[0]为[]运算符,从基类返回引用?只是语法问题或更深层次的问题?
#include <array>
template<class T, int C>
struct Vec
{
typedef T value_type;
typedef unsigned index_type;
typedef unsigned size_type;
std::array<T, C> v;
template<typename ...Args>
explicit Vec(Args&&... args) : v({{args...}}) {}
Vec(std::array<T, C> const & o) : v(o) {}
value_type & operator [] (index_type i)
{
return v[i];
}
value_type const & operator [] (index_type i) const
{
return v[i];
}
};
template<class …Run Code Online (Sandbox Code Playgroud) 我有一个小段,生成一个如下所示的显示列表:
glNewList ( CDisplayList :: GetBaseList () + 2, GL_COMPILE );
{
glBegin ( GL_QUADS );
glPushMatrix ();
//
// Move to slightly lower position.
//
glTranslatef ( 0.0f, -1.1f, 0.0f );
glColor4f ( 1.0f, 1.0f, 1.0f, 1.0f );
//
// Front facing.
//
glTexCoord2f ( m_TexLeft, m_TexBottom ); glVertex3f ( m_Start, -0.1f, 0.0f );
glTexCoord2f ( m_TexRight, m_TexBottom ); glVertex3f ( m_End, -0.1f, 0.0f );
glTexCoord2f ( m_TexRight, m_TexTop ); glVertex3f ( m_End, -1.3f, 0.0f );
glTexCoord2f ( m_TexLeft, …Run Code Online (Sandbox Code Playgroud) 以下用于使用Visual Studio 2013进行编译,但现在在Visual Studio 2015中提供错误"尝试引用已删除的函数".
template<typename... Args>
void operator()(Args && ... args)
{
auto guard = std::lock_guard<std::mutex>(Mutex);
{
Clean();
for (auto const & listener : Listeners)
{
if(auto locked = std::get<0>(listener).lock())
std::get<1>(listener)(args...);
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误是第一个声明:
auto guard = std::lock_guard<std::mutex>(Mutex)
Run Code Online (Sandbox Code Playgroud)
Mutex只是一个std :: mutex.任何人都可以解释为什么现在这是一个错误?
编辑:我也在这里使用这行代码:
auto converter = std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>();
Run Code Online (Sandbox Code Playgroud) 我想要一个合理可靠的线程计时器,因此我编写了一个计时器对象,该对象在线程上触发std :: function。我希望此计时器能够在到达下一个刻度之前停止运行;:: sleep无法实现的功能(至少我不认为可以)。
所以我要做的是将条件变量放在互斥锁上。如果条件超时,我将触发该事件。如果发出条件信号,则退出线程。因此Stop方法需要能够使线程停止和/或中断其等待,我认为这是它现在正在做的事情。
但是,这有问题。有时线程不是joinable(),并且有时条件在超时后但进入等待状态之前发出信号。
我如何改善它并使之坚固?
以下是完整的仓库。这里的等待时间为10秒,但是程序应在创建Foo之后立即终止,然后立即销毁。有时会,但大多数时候不会。
#include <atomic>
#include <thread>
#include <future>
#include <sstream>
#include <chrono>
#include <iostream>
class Timer
{
public:
Timer() {}
~Timer()
{
Stop();
}
void Start(std::chrono::milliseconds const & interval, std::function<void(void)> const & callback)
{
Stop();
thread = std::thread([=]()
{
for(;;)
{
auto locked = std::unique_lock<std::mutex>(mutex);
auto result = terminate.wait_for(locked, interval);
if (result == std::cv_status::timeout)
{
callback();
}
else
{
return;
}
}
});
}
void Stop()
{
terminate.notify_one();
if(thread.joinable())
{
thread.join();
} …Run Code Online (Sandbox Code Playgroud) 鉴于以下内容,如何使我的类或结构与std :: round兼容?(我假设同样的事情会使它与std :: floor和std :: ceil一起使用).我可以这样做吗?
#include <cmath>
struct Rectangle
{
Rectangle(double _x1, double _x2, double _y1, double _y2) :
x1(_x1), y1(_y1), x2(_x2), y2(_y2)
{
}
double x1, y1, x2, y2;
};
int main(void )
{
auto r = Rectangle(10.3, 10.4, 10.5, 10.6);
r = std::round(r);
std::cout << r.x1 << "," << r.y1 << "," << r.x2 << "," << r.y2 << std::endl;
}
Run Code Online (Sandbox Code Playgroud)