请考虑以下代码:
#include <iostream>
#include <type_traits>
template<typename T> class MyClass
{
public:
MyClass() : myVar{0} {;}
void testIf() {
if (isconst) {
myVar;
} else {
myVar = 3;
}
}
void testTernary() {
(isconst) ? (myVar) : (myVar = 3);
}
protected:
static const bool isconst = std::is_const<T>::value;
T myVar;
};
int main()
{
MyClass<double> x;
MyClass<const double> y;
x.testIf();
x.testTernary();
y.testIf(); // <- ERROR
y.testTernary(); // <- ERROR
return 0;
}
Run Code Online (Sandbox Code Playgroud)
对于x(非常量),没有问题.但是,即使在编译时知道if/else中的条件,y(const数据类型)也会导致错误.
是否有可能在编译时不编译错误条件?
我刚刚在这里和这里询问了有关数组和值初始化的两个问题.但是使用这段代码,我迷路了:
#include <iostream>
#include <iomanip>
#include <array>
template <class T, class U = decltype(std::declval<T>().at(0))>
inline U f1(const unsigned int i)
{T x; return x.at(i);}
template <class T, class U = decltype(std::declval<T>().at(0))>
inline U f2(const unsigned int i)
{T x = T(); return x.at(i);}
int main()
{
static const unsigned int n = 10;
static const unsigned int w = 20;
for (unsigned int i = 0; i < n; ++i) {
std::cout<<std::setw(w)<<i;
std::cout<<std::setw(w)<<f1<std::array<int, n>>(i);
std::cout<<std::setw(w)<<f2<std::array<int, n>>(i); …Run Code Online (Sandbox Code Playgroud) 前几天,我发现这是可能的:
template <class T> struct base {};
struct derived: base<int> {};
int main()
{
// The base class template is accessible here
typename derived::base<double> x;
// from the comments, even this works
typename derived::derived::base<double>::base<int>::base<void> y;
}
Run Code Online (Sandbox Code Playgroud)
我没有回忆过在cppreference或C++教程中读过这个,或者这是在聪明的模板元编程技巧中被利用的(因为我确信它可以).我有几个问题:
c++ inheritance language-lawyer template-meta-programming c++11
目前我有两个功能:
template<typename Type> bool f(Type* x);
template<typename... List> bool f(std::tuple<List...>* x);
Run Code Online (Sandbox Code Playgroud)
有没有办法将这两个函数与一个额外的模板参数合并,该参数指示传递的类型是否为元组?
template<typename Type, bool IsTuple = /* SOMETHING */> bool f(Type* x);
Run Code Online (Sandbox Code Playgroud) 考虑以下:
inline unsigned int f1(const unsigned int i, const bool b) {return b ? i : 0;}
inline unsigned int f2(const unsigned int i, const bool b) {return b*i;}
Run Code Online (Sandbox Code Playgroud)
语法f2更紧凑,但标准是否保证f1并且f2严格等同?
此外,如果,如果我想编译器优化这个表达式b,并i在编译时已知,哪个版本我更喜欢哪个?
以下是否符合C++ 11标准(= default在类的定义之外)?
// In header file
class Test
{
public:
Test();
~Test();
};
// In cpp file
Test::Test() = default;
Test::~Test() = default;
Run Code Online (Sandbox Code Playgroud) 作为一个懒惰的开发人员,我喜欢使用这个技巧来指定一个默认函数:
template <class Type, unsigned int Size, class Function = std::less<Type> >
void arrange(std::array<Type, Size> &x, Function&& f = Function())
{
std::sort(std::begin(x), std::end(x), f);
}
Run Code Online (Sandbox Code Playgroud)
但是在一个非常特殊的情况下我遇到了一个问题,如下所示:
template <class Type, unsigned int Size, class Function = /*SOMETHING 1*/>
void index(std::array<Type, Size> &x, Function&& f = /*SOMETHING 2*/)
{
for (unsigned int i = 0; i < Size; ++i) {
x[i] = f(i);
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我希望默认函数相当于:( [](const unsigned int i){return i;}一个只返回传递值的函数).
为了做到这一点,我需要写什么而不是/*SOMETHING 1*/和/*SOMETHING 2*/?
当C++标准化委员会调查STL的修改时,要特别注意不引入ABI破坏性变化.
是什么导致ABI破坏以及什么不引入C++中的ABI破坏?((欢迎关注课程或文件的链接)
考虑三个值x, y, z.
获得中间值的公式是什么(不是平均值,而是既不是min也不是max)的值?
const double min = std::min(x, std::min(y, z));
const double mid = /* what formula here ? */
const double max = std::max(x, std::max(y, z));
Run Code Online (Sandbox Code Playgroud) 请考虑以下代码:
template <unsigned int N>
struct myclass
{
unsigned int f() {return N;}
unsigned int g() {static_assert(N > 0, ""); return N-1;}
};
Run Code Online (Sandbox Code Playgroud)
问题:我是否保证以下代码将编译:
myclass<0> c;
c.f();
Run Code Online (Sandbox Code Playgroud)
但以下不会:
myclass<0> c;
c.f();
c.g();
Run Code Online (Sandbox Code Playgroud) c++ ×10
c++11 ×8
templates ×3
abi ×1
algorithm ×1
arrays ×1
boolean ×1
class ×1
compilation ×1
compile-time ×1
constructor ×1
destructor ×1
if-statement ×1
inheritance ×1
lambda ×1
max ×1
min ×1
standards ×1
std-function ×1
tuples ×1
type-traits ×1