25 Temmuz 2013, PERŞEMBE
Nasıl C koşullu bir typedef yapmak
Böyle bir şey yapmaya çalışıyorum:
#include <iostream>
#include <random>
typedef int Integer;
#if sizeof(Integer) <= 4
typedef std::mt19937 Engine;
#else
typedef std::mt19937_64 Engine;
#endif
int main()
{
std::cout << sizeof(Integer) << std::endl;
return 0;
}
ama bu hata alıyorum:
error: missing binary operator before token "("
Nasıl doğru koşullu typedef yapabilir miyim?
CEVAP
25 Temmuz 2013, PERŞEMBE
std::conditional
meta-fonksiyonu C 11 kullanın.
#include <type_traits> //include this
typedef std::conditional<sizeof(int) <= 4,
std::mt19937,
std::mt19937_64>::type Engine;
Eğer sizeof
kullandığınız tür bir şablon parametresi, T
, demek ise o zaman typename
gibi: kullanmanız gerektiğini unutmayın
typedef typename std::conditional<sizeof(T) <= 4, // T is template parameter
std::mt19937,
std::mt19937_64>::type Engine;
Ya Engine
T
bağlı olun:
template<typename T>
using Engine = typename std::conditional<sizeof(T) <= 4,
std::mt19937,
std::mt19937_64>::type;
esnekşimdi bu gibi kullanabilirsiniz: çünkü
Engine<int> engine1;
Engine<long> engine2;
Engine<T> engine3; // where T could be template parameter!
Bunu Paylaş:
Nasıl div içeriğini daha büyük değil y...
Nasıl dava Vim / küçük harf duyarlı ar...
Nasıl twitter bootstrap menü hover yer...
Sekmeler arasında geçiş yapmak için na...
Nasıl bir 'seçmek için bir yer tu...