我想初始化 ValueTuples 的静态只读数组,我想使用此答案中的方法:
var tupleList = new (int Index, string Name)[]
{
(1, "cow"),
(5, "chickens"),
(1, "airplane")
};
Run Code Online (Sandbox Code Playgroud)
但它不适用于静态成员,我必须声明 tupleList 的类型。我可以像这样将其作为元组来执行,但我不知道如何将其作为 ValueTuple 来执行:
static readonly Tuple<uint, string>[] tupleList= new Tuple<uint, string>[]
{
new Tuple<uint, string>(0x1, "string1"),
...
};
Run Code Online (Sandbox Code Playgroud)
但如果我能找出正确的类型,我更愿意使用更干净的格式,到目前为止,我已经尝试了多种类型,但没有成功。
我从那个答案中获取代码-https : //stackoverflow.com/a/9286697/2674303
我创建当前主题的原因是我不明白为什么该代码会导致死锁:
public class Lock implements Runnable {
static {
System.out.println(Thread.currentThread().getId() + "# Getting ready to greet the world");
try {
System.out.println(Thread.currentThread().getId() + "# before lock creation");
Lock target = new Lock();
System.out.println(Thread.currentThread().getId() + "# after lock creation");
Thread t = new Thread(target);
t.start();
System.out.println(Thread.currentThread().getId() + "# awaiting thread finish");
t.join();
System.out.println(Thread.currentThread().getId() + "# static block finished");
} catch (InterruptedException ex) {
System.out.println("won't see me");
}
}
public static void main(String[] args) {
System.out.println(Thread.currentThread() + "Hello World! "); …Run Code Online (Sandbox Code Playgroud) java concurrency multithreading deadlock static-initialization
这是一个简单的代码模式,在我们的代码库中一直困扰着我:
// in some_header_file.h
enum Color {
red = 0,
green,
blue,
max_item
};
// in some_other_file.cpp
static const char * color_names[max_item] = {
"red",
"green",
"blue"
};
std::string get_color_name(Color c) {
if ((c >= 0) && (c < max_item)) return color_names[c];
return "Unknown color";
}
Run Code Online (Sandbox Code Playgroud)
...上面的一切都很好,直到有一天,一些粗心的程序员(好吧,通常是我)过来并将一种新颜色(例如yellow)插入到Colors枚举中some_header_file.h(就在 之前max_item),但忘记还添加新颜色的字符串(例如)到中数组"yellow"的末尾。color_names[max_item]some_other_file.cpp
发生这种情况后,代码可以正常编译,没有错误或警告,但将来的调用get_color_name(yellow)(最好的情况下)不会返回预期的结果,或者(最坏的情况下)调用未定义的行为,甚至可能崩溃。
我希望在编译时捕获这个错误,这样我就可以避免在更新枚举时引入运行时错误。C++ 中有没有一种方法可以在编译时强制执行初始化字符串的数量color_names必须等于数组的长度(即max_item)?
我的盘子里有一个错误,用于在我们的库之一中查找和重写静态变量,该变量占用了我们应用程序的启动时间。我不熟悉库代码库,并要求良好的启发式/技术/grep 命令/等。这会减轻我识别所述静态变量位置的任务吗?
(PS 我已经在代码库中搜索static;不用说这是一个冗长的结果。)
更新:错误报告只是指出“库 XYZ 在静态初始化时需要 N 毫秒”;我没有关于静态变量的更多信息。我没有分析日志,但我会看看是否可以从错误报告者那里获取它们。
我有一个或多或少遵循这种模式的结构:
struct sTruct {
int count;
struct {
int A;
int B;
int C;
} array[]; //count is the size of this array
};
Run Code Online (Sandbox Code Playgroud)
我希望能够使用以下语法初始化它们:
sTruct gInit1 = { 2, { {1,2,3},{4,5,6} }};
Run Code Online (Sandbox Code Playgroud)
实际上,初始化语法(或者更确切地说,它的紧凑性)比特定的结构布局更重要.我无法访问标准容器(嵌入式平台),但如果需要,我可以复制它们的一些行为.
在最终形式中,我想一次初始化大约300个这些sTruct容器的数组,只是为了添加一个更高级别的括号.
我写了一段代码:
public class Child{
int y ;
private static final int z = getZ();
static {
System.out.println("The value of z is "+z);
}
public int getX(){
System.out.println("get x");
return 10;
}
public int getY(){
Child ch = new Child();
System.out.println("get y");
ch.y = getX();
return y;
}
public static int getZ(){
System.out.println("get z");
return new Child().getY();
}
public Child(){
System.out.println("Child constructor");
}
public static void main(String...args){
Child ch = new Child();
System.out.println("the value of z in main is "+z);
} …Run Code Online (Sandbox Code Playgroud) 通过查看以下代码,我们注意到初始化名为的静态数据成员非常容易cnt:
template<typename T> struct Base { static int cnt; };
template<typename T> int Base<T>::cnt = 0;
int main() { }
Run Code Online (Sandbox Code Playgroud)
无论如何,我正在与variadic_template静态数据成员斗争,因为我无法用它们来制作它.请考虑以下代码:
template<typename...> struct Base;
template<> struct Base<> { static int cnt; };
int main() { }
Run Code Online (Sandbox Code Playgroud)
首先,我尝试了最明显的事情,至少是对我来说最明显的事情:
template<typename... T> int Base<T...>::cnt = 0;
Run Code Online (Sandbox Code Playgroud)
它回来了error: template definition of non-template ‘int Base<T>::cnt’,我立刻意识到我的尝试确实没有意义.那就是说,之后我有点疑惑,因为对我来说这是不对的,这是正确的语法.
我尝试了以下方法:
template<> int Base<>::cnt = 0;
// ... and ...
template<> int Base::cnt = 0;
Run Code Online (Sandbox Code Playgroud)
随着错误error: template definition of non-template ‘unsigned int Base<T>::cnt’ …
我使用静态arraylist创建了一个Coin类,它存储了所创建的类的每个实例,但我需要用初始实例启动该列表,而且我没有想到如何在不添加它的情况下执行它(因为冗余代码) ), 有什么建议?
public class Coin {
private static ArrayList<String> coinNames = new ArrayList<>();
private static ArrayList<String> coinAbbreviations = new ArrayList<>(Arrays.asList("CLP"));
private static ArrayList<Coin> coins =
new ArrayList<>(Arrays.asList(new Coin("Pesos chilenos", "CLP", 1f, "CLP")));
private static HashMap<String,Float> exchangeRates;
private String coinName;
private String coinAbbreviation;
private Float coinValue;
private String unit;
public Coin(String coinName, String coinAbbreviation, Float coinValue, String unit) {
assert !coinAbbreviations.contains(coinAbbreviation) : "Coin abbreviation already used";
assert coinAbbreviations.contains(unit) : "Coin unit non existent.";
assert !coinNames.contains(coinName) : "Coin name already used."; …Run Code Online (Sandbox Code Playgroud) 我是 Rust 新手。我正在尝试在库中创建一个静态变量,DATA以便Vec<u8>在编译库后对其进行初始化。然后我将该库包含在主代码中,希望能够DATA直接使用而无需再次调用init_data()。这是我尝试过的:
my_lib.rs:
use lazy_static::lazy_static;
pub fn init_data() -> Vec<u8> {
// some expensive calculations
}
lazy_static! {
pub static ref DATA: Vec<u8> = init_data(); // supposed to call init_data() only once during compilation
}
Run Code Online (Sandbox Code Playgroud)
主要.rs:
use my_lib::DATA;
call1(&DATA); // use DATA here without calling init_data()
call2(&DATA);
Run Code Online (Sandbox Code Playgroud)
但事实证明,init_data()仍然是在调用main.rs。这段代码有什么问题?
更新:正如 Ivan C 指出的那样,lazy_static它不在编译时运行。那么,“预加载”数据的正确选择是什么?
假设我有一个使用自定义类加载器加载的类
Class<?> clz = myClassLoader.loadClass("classLoaders.Test");
Run Code Online (Sandbox Code Playgroud)
如何在不创建实例,了解其成员或使用反射的情况下初始化类(运行其静态初始化程序)?
c++ ×4
java ×4
arrays ×2
arraylist ×1
c# ×1
c++11 ×1
classloader ×1
concurrency ×1
deadlock ×1
enums ×1
java-8 ×1
launch-time ×1
lazy-static ×1
rust ×1
search ×1
static ×1
templates ×1
tuples ×1
variables ×1