compile-time

template metafunction for detecting template specialisations

放肆的年华 提交于 2019-12-01 01:46:04
问题 Inspired by this question, i'm wondering if there is some compile-time check one can introduce to detect if two given template instantiations: template <typename T> class Templ... typedef Templ<std::string> stringInstance; typedef Templ<double> doubleInstance; are constructed from the same definition, or if they are built from different specializations of the Templ template so basically the hypothetical template function will behave like this: template <typename T> class Templ {} template <>

How to tell if class contains a certain member function in compile time [duplicate]

▼魔方 西西 提交于 2019-11-30 22:50:44
Possible Duplicate: Is it possible to write a C++ template to check for a function's existence? say there are 2 classes: struct A{ int GetInt(){ return 10; } }; struct B{ int m; }; I want to use object of type A or B in following function tempate< typename T > int GetInt( const T & t ) { //if it's A, I'll call: return t.GetInt(); //if its' B, I'll call: return t.m; } Now, because there are whole bunch of classes, some contain GetInt(), some don't, I don't want to write specialization for each type, I only want to distinguish them by ' containing GetInt() or not in compile time ', how should I

Compile time computing of number of bits needed to encode n different states

淺唱寂寞╮ 提交于 2019-11-30 18:55:12
Edit: In the initial question had a wrong formula and the algorithm tried was doing something completely different than what was intended. I apologise and I decided to rewrite the question to eliminate all the confusion. I need to compute at compile time (the result will be used as a non-type template parameter) the minimum number of bits needed to store n different states: constexpr unsigned bitsNeeded(unsigned n); or via template The results should be: +-----------+--------+ | number of | bits | | states | needed | +-----------+--------+ | 0 | 0 | * or not defined | | | | 1 | 0 | | | | | 2 |

Can I separate creation and usage locations of compile-time strategies?

江枫思渺然 提交于 2019-11-30 17:57:39
问题 #include <iostream> #include <vector> #include <algorithm> #include <sstream> using namespace std; struct SubAlgorithm1 { void operator () (int /*i*/) { cout << "1" << endl; } }; struct SubAlgorithm2 { void operator () (int /*i*/) { cout << "2" << endl; } }; template<typename SubAlgorithm, typename Collection> void Alrogirthm(SubAlgorithm& f, Collection& stuff) { // In my code f is invoked ~ 1e9 times (it's a loop that is executed ~ // 1e6 times, and stuff.size() is ~1000). The application

Output compile time stamp in Visual C++ executable?

戏子无情 提交于 2019-11-30 17:25:00
How can I insert compilation timestamp information into an executable I build with Visual C++ 2005? I want to be able to output something like this when I execute the program: This build XXXX was compiled at dd-mm-yy, hh:mm. where date and time reflect the time when the project was built. They should not change with each successive call of the program, unless it's recompiled. Though not your exact format, DATE will be of the format Mmm dd yyyy, while TIME will be of the format hh:mm:ss. You can create a string like this and use it in whatever print routine makes sense for you: const char

Some const char * are unavailable at compile time?

早过忘川 提交于 2019-11-30 17:11:43
Let's suppose we have a template function with non-type parameter of const char * like this: template <const char * MESSAGE> void print() { std::cout << MESSAGE << '\n'; } Using this template wouldn't be a problem as log as the MESSAGE can be deduced at compile-time, so the following uses are legal: namespace { char namespace_message[] = "Anonymous Namespace Message"; constexpr char namespace_constexpr_message[] = "Anonymous Namespace Constexpr Message"; } char message[] = "Message"; constexpr char constexpr_message[] = "Constexpr Message"; int main() { print<namespace_message>(); print

In PHP, what is meant by compile-time and run-time? [duplicate]

走远了吗. 提交于 2019-11-30 17:10:02
This question already has an answer here: Is PHP compiled or interpreted? 8 answers PHP is an interpreted language, not compiled. Yet I have come across a book that mentions stuff happening in PHP at compile time, and the PHP manual states that declaring a const happens at compile-time . How is the term compile-time used in relation to PHP since PHP isn't compiled? If it is just meant as "when the script is read and translated into the interpreters subroutines", then what is the difference between the terms compile-time and run-time? PHP source code goes through a step where it is compiled

Angular, compile and create components at runtime

戏子无情 提交于 2019-11-30 16:12:36
问题 I'm trying to make a document generation tool in angular and I'm hitting a challenge with how I would allow a user to dynamically create content. My components I want to create could have arbitrary models and behavior so I don't think I could use a shared component. The components I'm describing would not exist at compile time. I see some documentation for rendering dynamic components. However it mentions that you must list the "dynamic" component in entryComponents in the ngModule . which

sizeof(value) vs sizeof(type)?

Deadly 提交于 2019-11-30 15:21:25
Considering : double data; double array[10]; std::vector<int> vec(4, 100); MyClass myclass; Is there a difference between : sizeof(double); sizeof(double[10]); sizeof(std::vector<int>); sizeof(MyClass); and sizeof(data); sizeof(array); sizeof(vec); sizeof(myclass); Are the two syntaxes different or strictly equivalent ? Are all of them evaluated at compile-time ? If not, which one is evaluated at run-time ? The only differences are in syntax and convenience. Syntactically, you're allowed to leave out the parentheses in one case, but not the other: double d; sizeof(double); // compiles sizeof(d

Unexpected non-constant std::initializer_list

霸气de小男生 提交于 2019-11-30 15:07:53
问题 I was toying a little bit with the indices trick to see where I could go to with and came across a strange error... First, the plain not-so-old indices: template<std::size_t...> struct indices {}; template<std::size_t N, std::size_t... Indices> struct make_indices: make_indices<N-1, N-1, Indices...> {}; template<std::size_t... Indices> struct make_indices<0, Indices...>: indices<Indices...> {}; I created a compile-time array class derived from a std::initializer_list and had it indexable