compile-time

How to check at compile-time if a function that can be called with a specific set of arguments exists?

亡梦爱人 提交于 2019-11-30 14:20:45
问题 This is different from checking if a specific function is defined. Here, for this check to return true , the function has to be defined and passing the arguments of a certain type should result in a valid call. Example: for a function f and an argument of type T && , the check should return true if f is a valid function that accepts—either directly or through implicit conversion—an argument of type T && . void f(int &) {}; int main(int argc, char **av) { isFunctionCallable<int>(f); // true

compile-time function for checking type equality

北城余情 提交于 2019-11-30 13:46:01
问题 I need to implement self contained compile-time function for checking type equality (function template without arguments bool eqTypes<T,S>() ). self contained means not relying on library. I'm not good in all this. That's what I tried, but it's not what I need. template<typename T> bool eq_types(T const&, T const&) { return true; } template<typename T, typename U> bool eq_types(T const&, U const&) { return false; } 回答1: It's quite simple. Just define a type trait and a helper function:

Unexpected non-constant std::initializer_list

試著忘記壹切 提交于 2019-11-30 13:20:47
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 (assume that N3471 is support by your compiler. It will be in the next standard anyway). Here it is:

How to check at compile-time if a function that can be called with a specific set of arguments exists?

微笑、不失礼 提交于 2019-11-30 10:14:25
This is different from checking if a specific function is defined. Here, for this check to return true , the function has to be defined and passing the arguments of a certain type should result in a valid call. Example: for a function f and an argument of type T && , the check should return true if f is a valid function that accepts—either directly or through implicit conversion—an argument of type T && . void f(int &) {}; int main(int argc, char **av) { isFunctionCallable<int>(f); // true because `int i; f(i);` is valid. isFunctionCallable<int &&>(f); // false because `int i; f(std::move(i));

compile-time function for checking type equality

随声附和 提交于 2019-11-30 08:29:37
I need to implement self contained compile-time function for checking type equality (function template without arguments bool eqTypes<T,S>() ). self contained means not relying on library. I'm not good in all this. That's what I tried, but it's not what I need. template<typename T> bool eq_types(T const&, T const&) { return true; } template<typename T, typename U> bool eq_types(T const&, U const&) { return false; } It's quite simple. Just define a type trait and a helper function: template<typename T, typename U> struct is_same { static const bool value = false; }; template<typename T> struct

Check for framework's existence at compile time?

这一生的挚爱 提交于 2019-11-30 06:36:53
I'm working on an open-source project that can optionally use a closed-source framework. If the closed-source framework is included in the project, there will be additional functionality. But if the framework isn't included in the project, the project should still compile properly. How do I check at compile-time if the framework is included in the project? Basically, I want to do something like this: #ifdef _MY_FRAMEWORK_EXISTS #import <MyFramework/MyFramework.h> #endif I've seen older questions from 2 years ago like this one , but no answer has surfaced so I might be missing something new now

Output compile time stamp in Visual C++ executable?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-30 00:55: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. 回答1: Though not your exact format, DATE will be of the format Mmm dd yyyy, while TIME will be of the format hh:mm:ss.

Some const char * are unavailable at compile time?

限于喜欢 提交于 2019-11-30 00:37:59
问题 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";

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

独自空忆成欢 提交于 2019-11-30 00:31:00
问题 This question already has answers here : Is PHP compiled or interpreted? (8 answers) Closed 5 years ago . 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

Why does the C++ compiler makes it possible to declare a function as constexpr, which can not be constexpr?

↘锁芯ラ 提交于 2019-11-29 15:02:35
Why does the C++ compiler makes it possible to declare a function as constexpr, which can not be constexpr? For example: http://melpon.org/wandbox/permlink/AGwniRNRbfmXfj8r #include <iostream> #include <functional> #include <numeric> #include <initializer_list> template<typename Functor, typename T, size_t N> T constexpr reduce(Functor f, T(&arr)[N]) { return std::accumulate(std::next(std::begin(arr)), std::end(arr), *(std::begin(arr)), f); } template<typename Functor, typename T> T constexpr reduce(Functor f, std::initializer_list<T> il) { return std::accumulate(std::next(il.begin()), il.end(