我正在寻找一个实现以下功能的列表类型(伪代码):
list.init(5, 2, 6, 9);
list.add(1) // 2, 6, 9, 1
list.add(4) // 6, 9, 1, 4
list.add(8) // 9, 1, 4, 8
Run Code Online (Sandbox Code Playgroud)
将新元素添加到固定大小列表并弹出最旧的元素.对不起,我不知道这个概念的名称,所以我问你,名字是什么.;)
我在C++中的实现实际上是这样的:
std::deque<double> values(4);
void add(double value)
{
values.pop_front();
values.push_back(value);
}
Run Code Online (Sandbox Code Playgroud)
有没有比我更好的实现,也许是所有固定大小?
我想从字符串中推断出函数的参数类型.类似于printf所做的.
目前我做以下事情:
#include <utility>
// calculate the length of a literal string
constexpr int length(const char* str)
{
return *str ? 1 + length(str + 1) : 0;
}
struct Ignore {
};
template <char C1, char C2>
struct Type {
typedef Ignore type;
};
// %d -> int
template <>
struct Type<'%','d'> {
typedef int type;
};
// %f -> float
template <>
struct Type<'%','f'> {
typedef float type;
};
// Get type from string
template <const char * …Run Code Online (Sandbox Code Playgroud) 我想检查一个类的成员变量是否是静态的.使用std :: is_member_pointer适用于除引用成员之外的所有类型.
#include <type_traits>
struct A {
int foo;
};
struct B : A {};
struct C {
static int foo;
};
struct D : C {
};
struct E {
int &foo;
};
struct F {
static int &foo;
};
static_assert(std::is_member_pointer<decltype(&A::foo)>::value, "No");
static_assert(std::is_member_pointer<decltype(&B::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&C::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&D::foo)>::value, "No");
// Fail to compile:
static_assert(std::is_member_pointer<decltype(&E::foo)>::value, "No");
static_assert(!std::is_member_pointer<decltype(&F::foo)>::value, "No");
Run Code Online (Sandbox Code Playgroud)
我理解错误,指针不能指向引用成员.但是如何避免它并仍然区分它是静态还是非静态变量?有什么想法吗?
今天我遇到了这段代码:
int main() {
struct Foo {};
struct Bar {};
Foo(b)(int (Bar*c)); // ?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我完全不知道发生了什么.我的编译器(VC14)警告我未使用的原型函数?
这行做什么(声明一个函数:哪个名称,什么参数和返回类型?如何调用它?)
Foo(b)(int (Bar*c));
Run Code Online (Sandbox Code Playgroud)
提前谢谢你的帮助!
我知道有很多可能的方法可以检测一个类是否具有特定的功能但是它们的确不适用于我的确切情况.除了继承函数之外,我当前检查正确成员函数的实现是否有效.
#include <type_traits>
template<typename T>
class HasFoo {
template <typename U, int (U::*)(float)>
struct Check;
template <typename U>
static std::true_type Test(Check<U, &U::foo> *);
template <typename U>
static std::false_type Test(...);
public:
static constexpr bool value = decltype(Test<T>(0))::value;
};
struct A {
int foo(float);
};
struct B : public A {
};
struct C {
unsigned int foo(double);
};
struct D {
static int foo(float);
};
static_assert(HasFoo<A>::value, "A should have foo.");
static_assert(HasFoo<B>::value, "B should inherit foo from A.");
static_assert(!HasFoo<C>::value, "C should not …Run Code Online (Sandbox Code Playgroud) GCC无法将某些表达式评估为常量.然而,Clang很不错.
/*
*/
constexpr int foo(const int * array)
{
if (array == nullptr) // Error: '(((const int*)(& array)) == 0u)' is not a constant expression
{
return 0;
}
return 1;
}
constexpr int bar()
{
int array[100] = {};
return foo(array);
}
static_assert(bar() == 1, "outch..."); // Does not compile. See above.
static_assert(foo(nullptr) == 0, "okay");
constexpr int i[100] = {};
static_assert(foo(i) == 1, "okay");
Run Code Online (Sandbox Code Playgroud)
也行不通:
constexpr int foobar()
{
int array[100] = {};
int *ar = array; …Run Code Online (Sandbox Code Playgroud) 我发现了一个bug在GCC 6和7(未在GCC 5)内constexpr功能,这导致不同的结果,如果任一函数获取在编译时(错误的结果),或运行时(正确结果)的计算结果.
#include <iostream>
constexpr int bar(int *b) {
int i = 0;
b[i++] = 1; // GCC produce here an failure.
return 0;
}
constexpr int foo()
{
int tmp[] = {0};
bar(tmp);
return tmp[0];
}
constexpr int cexprI = foo();
int main()
{
std::cout << cexprI << " " << foo() << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是数组访问中的增量(也发生在减量)操作.
常量表达式的编译时结果为0(错误),运行时结果为1(正确).
任何人都可以确认此错误并报告给:https://gcc.gnu.org/bugzilla/
我无法在那里创建一个帐户User account creation has been restricted..我联系了管理员,但对我来说这个问题很重要.所以它也想通知你.谢谢!
std::conditional_variable_any 的实现需要(在gcc和clang 中)一个 std::shared_ptr。
在wait方法内部,互斥锁的生命周期将扩展到本地范围。
template<typename _Lock>
void
wait(_Lock& __lock)
{
shared_ptr<mutex> __mutex = _M_mutex; // <-- Extend lifetime of mutex.
unique_lock<mutex> __my_lock(*__mutex);
_Unlock<_Lock> __unlock(__lock);
// *__mutex must be unlocked before re-locking __lock so move
// ownership of *__mutex lock to an object with shorter lifetime.
unique_lock<mutex> __my_lock2(std::move(__my_lock));
_M_cond.wait(__my_lock2);
}
Run Code Online (Sandbox Code Playgroud)
我想知道,为什么我们需要这个?只要conditional_variable_any对象存在,互斥锁就存在。std::mutex 还不够吗?
考虑以下类型特征:
template<typename T, typename = void>
struct has_begin : std::false_type {};
template<typename T>
struct has_begin<T, std::void_t<
decltype(std::begin(std::declval<std::add_lvalue_reference_t<T>>()))
>> : std::true_type {};
Run Code Online (Sandbox Code Playgroud)
为什么此特征不考虑我的用户定义的重载std::begin?
namespace std {
void begin(foo&) {}
}
int main() {
static_assert(has_begin<foo>::value); // Fails.
foo f;
std::begin(f); // Works.
}
Run Code Online (Sandbox Code Playgroud)
有趣的观察:
decltype(std::begin(std::add_lva... -> decltype(begin(std::add_lva...
如果free函数begin与foo位于相同的名称空间中,则它可以工作:
void begin(foo) {
}
Run Code Online (Sandbox Code Playgroud)
但是对于std ::之外的任何类都失败,具体取决于:
void begin(foo) {
}
Run Code Online (Sandbox Code Playgroud)
因为ADL查找不适用于其他名称空间的模板。
std::begin(foo&)在不更改包含顺序的情况下,我可以做些什么来支持我的类型特征?
否则,我必须同时支持这两个世界-为std :: begin和ADL begin()编写类型特征。
在我的函数中,我已经做了这样的事情(建议在这里):
auto get_begin() { …Run Code Online (Sandbox Code Playgroud) 在我们的项目中,我们使用CMake和两个不同的构建目标:Debug和Release.
Clion确实提供了两个额外的构建目标:RelWithDebInfo和MinSizeRel.现在,当Clion创建CMake缓存(对于所有4,我们只使用2)时,它会失败,因为除了调试或发布之外我们不允许其他构建目标.
您的第一个解决方法是在主CMakeList.txt上使用它:
if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug|Release")
return()
endif()
Run Code Online (Sandbox Code Playgroud)
但默认情况下是否可以从Clion禁用这两个配置?