Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单件能编译通过么? #3

Closed
yongxin-ms opened this issue Sep 15, 2021 · 1 comment
Closed

单件能编译通过么? #3

yongxin-ms opened this issue Sep 15, 2021 · 1 comment

Comments

@yongxin-ms
Copy link

`template <class... P>
static T &instance(P... args)
{
if (s_ins)
{
return *s_ins;
}

			std::call_once(s_once, [&]() {
				s_ins = new T(args...);
			});
			return *s_ins;
		}

		static T &instance()
		{
			if (s_ins)
			{
				return *s_ins;
			}
			std::call_once(s_once, [&]() {
				s_ins = new T();
			});
			return *s_ins;
		}

`

单件写成这样应该编不过的吧?估计得写成宏才能支持多种参数的情况吧,或者能把参数保存下来么?第一次使用完整的参数,后面使用保存下来的参数?

@cageq
Copy link
Owner

cageq commented Sep 18, 2021

use std::tuple and std::apply or add init method to singleton class

@cageq cageq closed this as completed Sep 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants