-
Notifications
You must be signed in to change notification settings - Fork 45
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
Rust static variables as C++ non-type template parameter #80
Comments
This isn't valid C++ code. The same thing wouldn't compile in plain C++ either. #include <cstdint>
template <int32_t N>
void Add() {}
static int32_t NUMBER = 42;
int main() {
Add<NUMBER>();
} $ clang++ main.cc
main.cc:9:3: error: no matching function for call to 'Add'
Add<NUMBER>();
^~~~~~~~~~~
main.cc:4:6: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'N'
void Add() {}
^ $ g++ main.cc
main.cc: In function ‘int main()’:
main.cc:9:7: error: the value of ‘NUMBER’ is not usable in a constant expression
9 | Add<NUMBER>();
| ^~~~~~
main.cc:6:16: note: ‘int32_t NUMBER’ is not const
6 | static int32_t NUMBER = 42;
| ^~~~~~
main.cc:9:15: error: no matching function for call to ‘Add<NUMBER>()’
9 | Add<NUMBER>();
| ^
main.cc:4:6: note: candidate: ‘template<int N> void Add()’
4 | void Add() {}
| ^~~ |
Thank you for the reply @dtolnay (btw, I love your work!) I agree with you that the example C++ snippet that you have provided is not valid C++. However, I'm not trying to produce that sort of code. This is the sort of valid C++ that I hope to produce: #include <cstdint>
template <int32_t N>
void Add() {}
static const int32_t NUMBER = 42;
int main() {
Add<NUMBER>();
} How can I pass a Rust variable (whose value is known at compile time) to the C++ side such that it can be used as a non-type template parameter? |
What is the real use case you have in mind for this feature? For example, we could imagine an annotation such as: #[cpp::export_static(as "uint32_t")]
static NUMBER: u32 = 42; Which would tell cpp_build to generate This would have the following drawback:
|
Thanks for the reply @ogoffart. I think you're right that I need to take a step back and make sure my strategy makes sense. My ultimate goal is to build a Rust library which wraps the SDSL C++ library. SDSL provides data structures such as the template<uint8_t t_width>
class int_vector There are other data structures which are a more complicated. Their templates are parameterized on types. For example the wavelet tree class for integer sequences: template<class t_bitvector = bit_vector,
class t_rank = typename t_bitvector::rank_1_type,
class t_select = typename t_bitvector::select_1_type,
class t_select_zero = typename t_bitvector::select_0_type>
class wt_int I think that what you've suggested above ( How do you think I should go about writing a wrapper for this library? |
Hello!
I'm trying to use a Rust static variable as a C++ non-type template parameter. Is this possible?
This is a simple example which outlines what I'm trying to do:
I'm trying to replace
replace_me_with_number
withNUMBER
.src/tmp.h
is just:Thank you for your help.
The text was updated successfully, but these errors were encountered: