Windows仅向Windows Vista提供GetTickCount,并从该操作系统开始提供GetTickCount64.如何通过调用不同的函数来编译C程序?
如何让C编译器检查是否在包含的头文件中声明了一个函数,并根据该特定函数是否可用来编译代码的不同部分?
#if ??????????????????????????????
unsigned long long get_tick_count(void) { return GetTickCount64(); }
#else
unsigned long long get_tick_count(void) { return GetTickCount(); }
#endif
Run Code Online (Sandbox Code Playgroud)
寻找工作样本文件而不仅仅是提示.
编辑:我在(64位)Windows 7 RC上使用minGW中的gcc 3.4.5尝试了以下操作,但它没有帮助.如果这是MinGW问题,我该如何解决这个问题?
#include <windows.h>
#if (WINVER >= 0x0600)
unsigned long long get_tick_count(void) { return 600/*GetTickCount64()*/; }
#else
unsigned long long get_tick_count(void) { return 0/*GetTickCount()*/; }
#endif
Run Code Online (Sandbox Code Playgroud) 我发现类似的问题和答案像这一个.但是,正如我尝试的那样,如果测试成员直接在被测试的类中定义,则此SFINAE测试仅成功.例如以下,类B,D1打印HAS而另外两个打印NOT HAS.有没有一种方法来确定,如果一个类有一个成员,无论是由自己定义,或基类和基类的名称没有在这种情况下,已知的.我的动机是,我想编写一个泛型函数,如果它存在,将调用某个方法(从基础或不基础,参数的类型是通用的,留下其可能的基类型).
#include <iostream>
class HasFoo
{
public :
typedef char Small;
typedef struct {char; char;} Large;
template <typename C, void (C::*) ()> class SFINAE {};
template <typename C> static Small test (SFINAE<C, &C::foo> *)
{
std::cout << "HAS" << std::endl;
}
template <typename C> static Large test (...)
{
std::cout << "NOT HAS" << std::endl;
}
};
class B
{
public :
void foo () {}
};
class D1 …Run Code Online (Sandbox Code Playgroud) 可能重复:
是否可以编写C ++模板来检查函数的存在?
我有一个f接收val类型T(模板化)值的函数。val 仅当类型具有此类成员函数时,才可以使用任何方法来调用该成员函数吗?
例:
struct Bar {
void foo() const {}
};
template<class T>
void f(T const& val) {
// Is there any way to call *only* if foo() is available on type T?
// SFINAE technique?
val.foo();
}
int main() {
Bar bar;
f(bar);
f(3.14);
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这听起来像是SFINAE技术,也许使用boost :: enable_if,但是我不知道如何在这里使它工作。请注意,我无法轻松更改Bar示例中的类型。我知道,如果Bar包含某些特定的typedef等会很容易,这表明该功能可用。
不用说,我不知道类型的集合T是f将被调用。有些具有foo成员函数,有些则没有。
我编写了一个异步作业队列类,它已经很好地工作了很长时间.它使用a std::vector作为底层集合来保留作业,然后按照您的预期稍后处理它们.当我添加一份工作时,它会push_back对此进行处理vector.
最近我决定要模仿它使用的底层集合类型以及我编写它的方式,这应该非常简单.它现在宣布如下:
template<typename J, typename CollectionT = std::vector<J>>
class async_jobqueue
{
public:
Run Code Online (Sandbox Code Playgroud)
只有一个障碍,对于矢量型容器我想把东西推到集合的末尾并调用push_back,我想要调用的定型容器insert.如何做出关于调用哪个的编译决定?或者有一个方便的适配器我可以使用?
这个问题本质上是来自另一个用户的这个问题的后续问题,它有一些很好的答案: 是否可以编写模板来检查函数的存在?
我想完全按照这个问题描述,除了我希望能够为构造函数做到这一点.例如,鉴于以下两种类型:
class NormalType
{
public:
NormalType()
{
std::cout << "NormalType::NormalType()" << std::endl;
}
};
class SpecialType
{
public:
SpecialType()
{
std::cout << "SpecialType::SpecialType()" << std::endl;
}
SpecialType(int someArg)
{
std::cout << "SpecialType::SpecialType(int someArg)" << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
这个辅助函数用于构造一个对象:
template<class T>
class ConstructHelper
{
public:
template<bool HasSpecialConstructor>
static T Construct()
{
return T();
}
template<>
static T Construct<true>()
{
return T(int(42));
}
};
Run Code Online (Sandbox Code Playgroud)
我希望能够编写如下代码:
NormalType normalType = ConstructHelper<NormalType>::Construct<has_special_constructor<NormalType>::value>();
SpecialType specialType = ConstructHelper<SpecialType>::Construct<has_special_constructor<SpecialType>::value>();
Run Code Online (Sandbox Code Playgroud)
NormalType::NormalType()调用所需结果的地方,并SpecialType::SpecialType(int someArg)调用.这里缺少的成分是关键has_special_constructor帮助器,它可以确定我们的特殊构造函数是否存在给定类型. …
我编写了这段代码来检查类类型是否具有begin函数.
struct foo //a simple type to check
{
int begin(){ return 0;}
};
struct Fallback
{
int begin(){ return 0;}
};
template<typename T>
struct HasfuncBegin : T,Fallback
{
typedef char one;
typedef int two;
template<typename X>
static one check(int (X::*)() = &HasfuncBegin<T>::begin);
template<typename X>
static two check(...);
enum :bool {yes = sizeof(check<T>())==1, no= !yes};
};
int main()
{
std::cout<< HasfuncBegin<foo>::yes;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪会产生错误:
error: call of overloaded 'check()' is ambiguous
enum {yes = sizeof(check<T>())==1, no= !yes}; …Run Code Online (Sandbox Code Playgroud) 我在 SO 上搜索了一下,很惊讶我没有找到任何类似的问题。如果已经回答了任何提示,我们很高兴。
我有一个定义了很多枚举类的代码库。其中一些指定了 totalNum 常量,例如
enum class Foo : int
{
a,
b,
c,
totalNum
}
Run Code Online (Sandbox Code Playgroud)
其他人没有这样的
enum class Bar : bool
{
oneOption,
otherOption
}
Run Code Online (Sandbox Code Playgroud)
然后我有一个基本上像这样的功能
template <class EnumClassType>
EnumClassType typeToEnum (typename std::underlying_type<EnumClassType>::type value)
{
// If you hit this assertion, the value is outside of the valid enum range
assert (isPositiveAndBelow (value, decltype (value) (EnumClassType::totalNum)));
return EnumClassType (value);
}
Run Code Online (Sandbox Code Playgroud)
虽然这对totalNum指定的枚举有效并且有意义,但我想跳过这个断言,以防枚举中没有这样的标识符。有没有办法做到这一点?代码库目前使用 C++ 14,但由于即将进行的编译器更改,也欢迎使用 C++ 17 解决方案。
可能重复:
是否可以编写C++模板来检查函数是否存在?
说有两个类:
struct A{ int GetInt(){ return 10; } };
struct B{ int m; };
Run Code Online (Sandbox Code Playgroud)
我想在以下函数中使用类型A或B的对象
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;
}
Run Code Online (Sandbox Code Playgroud)
现在,因为有一大堆类,一些包含GetInt(),一些不包含,我不想为每种类型编写特化,我只想在编译时通过' 包含GetInt()来区分它们',我该怎么做?
我正在寻找模板的帮助.我需要在模板中创建对特定类型有不同反应的函数.
它可能看起来像这样:
template <typename T>
class SMTH
{
void add() {...} // this will be used if specific function isn't implemented
void add<int> {...} // and here is specific code for int
};
Run Code Online (Sandbox Code Playgroud)
我也尝试在单个函数中使用typeid和swich通过类型,但对我不起作用.
我正在写一个contains()实用功能,并提出了这个问题.我的问题是:有没有更好的方法来选择正确的函数来处理呼叫?
template <class Container>
inline auto contains(Container const& c,
typename Container::key_type const& key, int) noexcept(
noexcept(c.end(), c.find(key))) ->
decltype(c.find(key), true)
{
return c.end() != c.find(key);
}
template <class Container>
inline auto contains(Container const& c,
typename Container::value_type const& key, long) noexcept(
noexcept(c.end(), ::std::find(c.begin(), c.end(), key))
)
{
auto const cend(c.cend());
return cend != ::std::find(c.cbegin(), cend, key);
}
template <class Container, typename T>
inline auto contains(Container const& c, T const& key) noexcept(
noexcept(contains(c, key, 0))
)
{
return contains(c, key, …Run Code Online (Sandbox Code Playgroud) c++ ×9
sfinae ×4
templates ×4
c++14 ×2
c ×1
c++17 ×1
compile-time ×1
decltype ×1
enum-class ×1
macros ×1