我在 MSVC++ 17 版本 15.5.5 中实现单例模式时遇到问题。我正在编译标志 /std:c++17 .
我的实现包含以下辅助类:
#pragma once
#include <cassert>
template<class T>
class Singleton : private T
{
public:
virtual ~Singleton() = default;
template<typename ... Targs>
static T & initInstance(Targs && ... args)
{
assert(instance == nullptr);
instance = new Singleton<T>(std::forward<Targs>(args)...); //The constructor of T might be inaccessible here so let our own ctor call it
return *instance;
}
static T & getInstance()
{
assert(instance != nullptr);
return *instance;
}
private:
template<typename ... Targs>
explicit Singleton(Targs && ... args)
: T{ std::forward<Targs>(args)... }
{}
static T * instance;
};
template<class T>
T * Singleton<T>::instance = nullptr;
此实现的一个目标是具有某种形式的惰性初始化,但没有多余的 if每次执行的语句 getInstance()叫做。只有第一次才有用,因为实例需要初始化,但之后 if只是开销。为此,我创建了函数 initInstance()你必须先打电话才能打电话getInstance() .我很清楚 assert()仅在 Debug模式下工作,但这对我的项目来说很好。
该类旨在按以下方式使用:
#include "Singleton.h"
#include <iostream>
class Foo
{
public:
virtual ~Foo() = default;
protected: //has no public ctor's
Foo(int i) //has no default ctor
: i{ i }
{
std::cout << "foo ctr " << i << "\n"; //Not printed if no ctor of Foo is found
}
private:
int i;
};
int main(int argc, char** argv)
{
Singleton<Foo>::initInstance(5); //Selects and executes Foo(int i).
//Singleton<Foo>::initInstance(); //Should not compile, yet it does. Calls no ctor of Foo at all.
Foo & theOnlyFoo = Singleton<Foo>::getInstance(); //or just use the return value of initInstance(5) to initialize this reference
//...
return 0;
}
问题:
如果我调用 Singleton<Foo>::initInstance();没有任何论据,即使Foo没有默认构造函数,代码仍然可以编译和运行。我本来期望实例化 Singleton<Foo> 的构造函数会失败,因为它调用 Foo具有给定参数的构造函数,但在查找时它不应该能够找到合适的构造函数。然而代码以某种方式编译。
当我执行代码时 Singleton<Foo> 的构造函数被调用但是 Foo 的构造函数本身不是。当我在 instance 之后的断点处停止执行时已初始化并检查其数据成员的值 i它是一些随机的巨大负值,表明根本没有执行任何代码来初始化 Foo 的实例。 .
只有在 Foo 没有合适的构造函数时才会发生这种情况。打电话。如果有,一切正常,Foo调用选定的构造函数。
最佳答案
正如评论中所建议的,这可能是一个编译器错误。我提交了错误报告。
我还针对我在评论中描述的内部编译器错误提交了错误报告。当我(错误地)输入 using T::T; 时发生内部编译器错误在 Singleton<T>::InitInstance()然后调用T s 构造函数。
关于c++ - 单例类找不到 ctor 但编译、运行并使实例未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49070263/
在我的gem中,我需要yaml并且在我的本地计算机上运行良好。但是在将我的gem推送到rubygems.org之后,当我尝试使用我的gem时,我收到一条错误消息=>"uninitializedconstantPsych::Syck(NameError)"谁能帮我解决这个问题?附言RubyVersion=>ruby1.9.2,GemVersion=>1.6.2,Bundlerversion=>1.0.15 最佳答案 经过几个小时的研究,我发现=>“YAML使用未维护的Syck库,而Psych使用现代的LibYAML”因此,为了解决
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在查看instance_variable_set的文档并看到给出的示例代码是这样做的:obj.instance_variable_set(:@instnc_var,"valuefortheinstancevariable")然后允许您在类的任何实例方法中以@instnc_var的形式访问该变量。我想知道为什么在@instnc_var之前需要一个冒号:。冒号有什么作用? 最佳答案 我的第一直觉是告诉你不要使用instance_variable_set除非你真的知道你用它做什么。它本质上是一种元编程工具或绕过实例变量可见性的黑客攻击
在我的应用程序中,我需要能够找到所有数字子字符串,然后扫描每个子字符串,找到第一个匹配范围(例如5到15之间)的子字符串,并将该实例替换为另一个字符串“X”。我的测试字符串s="1foo100bar10gee1"我的初始模式是1个或多个数字的任何字符串,例如,re=Regexp.new(/\d+/)matches=s.scan(re)给出["1","100","10","1"]如果我想用“X”替换第N个匹配项,并且只替换第N个匹配项,我该怎么做?例如,如果我想替换第三个匹配项“10”(匹配项[2]),我不能只说s[matches[2]]="X"因为它做了两次替换“1fooX0barXg
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个正在构建的应用程序,我需要一个模型来创建另一个模型的实例。我希望每辆车都有4个轮胎。汽车模型classCar轮胎模型classTire但是,在make_tires内部有一个错误,如果我为Tire尝试它,则没有用于创建或新建的activerecord方法。当我检查轮胎时,它没有这些方法。我该如何补救?错误是这样的:未定义的方法'create'forActiveRecord::AttributeMethods::Serialization::Tire::Module我测试了两个环境:测试和开发,它们都因相同的错误而失败。 最佳答案
我有用于控制用户任务的Rails5API项目,我有以下错误,但并非总是针对相同的Controller和路由。ActionController::RoutingError:uninitializedconstantApi::V1::ApiController我向您描述了一些我的项目,以更详细地解释错误。应用结构路线scopemodule:'api'donamespace:v1do#=>Loginroutesscopemodule:'login'domatch'login',to:'sessions#login',as:'login',via::postend#=>Teamroutessc
我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_
我正在阅读一本关于Ruby的书,作者在编写类初始化定义时使用的形式与他在本书前几节中使用的形式略有不同。它看起来像这样:classTicketattr_accessor:venue,:datedefinitialize(venue,date)self.venue=venueself.date=dateendend在本书的前几节中,它的定义如下:classTicketattr_accessor:venue,:datedefinitialize(venue,date)@venue=venue@date=dateendend在第一个示例中使用setter方法与在第二个示例中使用实例变量之间是
我收到格式为的回复#我需要将其转换为哈希值(针对活跃商家)。目前我正在遍历变量并执行此操作:response.instance_variables.eachdo|r|my_hash.merge!(r.to_s.delete("@").intern=>response.instance_eval(r.to_s.delete("@")))end这有效,它将生成{:first="charlie",:last=>"kelly"},但它似乎有点hacky和不稳定。有更好的方法吗?编辑:我刚刚意识到我可以使用instance_variable_get作为该等式的第二部分,但这仍然是主要问题。