问题描述
我有一个公共API函数(在库头中),该函数接受// remove any dates older than today function
(function ($) {
$.fn.removeOldDates = function () {
$("select[id^='pa_date_'] > option").each(function () {
const Now = new Date().getTime();
const time = this.text.split(/[\s/-]+/); // split the time,by / or -
const day = time[0];
const month = time[1] - 1;
const year = time[2];
const row_date = new Date();
const test = row_date.setFullYear(year,month,day);
// Now we have both times,we can compare them
if (Now > row_date.getTime() || this.text == "") {
// if the date is less that today or empty remove it from the dropdown
$("select[id^='pa_date_'] > option[value=" + this.value + "]").remove();
}
});
};
})(jQuery);
// re-order the dates function
(function ($) {
$.fn.orderDates = function () {
$(this).find("option:not(:first)")
.each(function (index,value) {
const t = this.textContent.split(/[\s/-]+/);
$(this).data( "_ts",new Date(t[2],t[1] - 1,t[0]).getTime() );
}).sort(function (a,b) {
return $(a).data("_ts") - $(b).data("_ts");
}).appendTo($(this));
return this;
};
})(jQuery);
// Map the date drop downs
$("select[id^=pa_date_]").each(function (index) {
$(this).removeOldDates();
$(this).orderDates();
});
,并且该函数的内部实现调用需要struct foo &
的第三方依赖项函数。
我不想将第三方依赖项引入公共标头中,因为该库将以二进制形式分发,并且此依赖项是实现细节。
但是,我不想将任何其他信息附加到struct bar *
上,而是希望能够执行foo
以减少我必须执行的副本数量(这是非常常见的称为函数)。
标准库是否具有reinterpret_cast<bar*>(&foo)
或其他某种机制来检查type_trait
和foo
的结构是否相同?我知道这将需要我进行更多的维护,以确保标头也与内部依赖项的类型定义相匹配,但这很好,因为不太可能很快改变。
最终我需要类似的东西
bar
解决方法
reinterpret_cast<bar*>(&f)
始终是UB。
如果没有严格的混叠,则可以使用c ++ 20的std::is_layout_compatible
,但是对此没有任何编译器支持。
您可以使用这种黑客手段:
template<typename T,typename U>
constexpr bool is_same_member_type(T foo::*,U bar::*) {
return std::is_same_v<T,U>;
}
#define HAS_SAME_MEMBER(foo_name,bar_name) (offsetof(foo,foo_name) == offsetof(bar,bar_name) && is_same_member_type(&foo:: foo_name,&bar:: bar_name))
// add more HAS_SAME_MEMBER calls if new members are added
static_assert(
sizeof(foo) == sizeof(bar) &&
std::is_standard_layout_v<foo> && std::is_standard_layout_v<bar> &&
HAS_SAME_MEMBER(a,c) &&
HAS_SAME_MEMBER(b,d),"foo must be structurally equivalent to bar"
);
#undef HAS_SAME_MEMBER