我们可以做这样的事情:
#include <iostream>
class Foo
{
public:
Foo() { std::cout << sizeof(*this) << '\n'; }
};
Run Code Online (Sandbox Code Playgroud)
在C标准中,我看到以下内容:
ISO/IEC 9899:2011
6.7.2.1结构和联合说明符
8 ...类型不完整,直到终止列表的}之后,然后完成.
但在C++标准版中我找不到任何类比.
sizeof运算符不应该应用于具有不完整类型的表达式,那么我们可以编写这样的代码吗?
我错了以下吗?
C++标准规定,指向函数和指向对象(以及返回)的指针之间的转换是通过实现定义的语义得到条件支持的,而所有C标准都认为这在所有情况下都是非法的,对吧?
void foo() {}
int main(void)
{
void (*fp)() = foo;
void* ptr = (void*)fp;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
ISO/IEC 14882:2011
5.2.10重新解释强制转换[expr.reinterpret.cast]
8有条件地支持将函数指针转换为对象指针类型(反之亦然).这种转换的含义是实现定义的,除非实现支持两个方向的转换,将一种类型的prvalue转换为另一种类型并返回,可能具有不同的cvqualification,将产生原始指针值.
我现在在C标准中找不到任何关于它的信息......
假设我们有以下代码:
template <typename T>
void foo(const T&);
int main()
{
foo("str");
}
Run Code Online (Sandbox Code Playgroud)
gcc 4.7.2,clang 3.2,icc 13.0.1
未命名的引用`void foo < char [4]>(char const(&)[4])'
MSVC-11.0
未解析的外部符号"void __cdecl foo < char const [4]>(char const(&)[4])"(?? $ foo @ $$ BY03 $$ CBD @@ YAXAAY03 $$ CBD @ Z)
请注意char[4]第一个输出和char const[4]第二个输出.
为什么?谁是对的?你能引用这个标准吗?
我怎么能在下周日睡觉前使用加强?我可以将boost::gregorian::date对象转换为boost::this_thread::sleep_until可以处理的东西吗?可能吗?
#include <boost/date_time.hpp>
int main()
{
boost::gregorian::date current_date(boost::gregorian::day_clock::local_day());
boost::gregorian::greg_weekday next_sunday_date(boost::gregorian::Sunday);
boost::gregorian::date next_weekday_date = next_weekday(current_date, next_sunday_date);
// ...
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码:
#include <algorithm>
#include <cctype>
#include <string>
int main()
{
std::string str;
str.erase(std::remove_if(str.begin(), str.end(), std::isspace), str.end());
}
Run Code Online (Sandbox Code Playgroud)
MSVC-11.0编译此代码没有任何错误,但gcc 4.7.2给出了以下错误:
main.cpp: In function ‘int main()’:
main.cpp:8:66: error: no matching function for call to ‘remove_if(std::basic_string<char>::iterator, std::basic_string<char>::iterator, <unresolved overloaded function type>)’
main.cpp:8:66: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from main.cpp:1:
/usr/include/c++/4.7/bits/stl_algo.h:1160:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
/usr/include/c++/4.7/bits/stl_algo.h:1160:5: note: template argument deduction/substitution failed:
main.cpp:8:66: note: couldn't deduce template parameter ‘_Predicate’
Run Code Online (Sandbox Code Playgroud)
我发现了这个问题,但根据cppreference,没有任何版本的函数接受两个参数.我也发现了这个问题,但根据cppreference(是的,再次),我看到只有一个std :: …
我正在尝试使用boost::asio::spawn像示例中的函数,但它在Release中给出了以下错误:
libboost_context-vc120-mt-s-1_55.lib(jump_i386_ms_pe_masm.obj):错误LNK2026:SAFESEH映像的模块不安全
很明显,我应该/SAFESEH:NO在项目的设置中设置选项,但我无法理解它实际上会做什么.这会如何影响程序中的异常处理行为(包括C++异常和SEH)?
顺便说一下,我正在使用MSVC-12.0.
c++ boost exception-handling visual-studio visual-studio-2013
我需要从中呈现视图控制器AppDelegate,所以我编写了以下代码:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let authViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as ViewController
if let keyWindow = UIApplication.sharedApplication().keyWindow {
keyWindow.rootViewController = authViewController
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,window和keyWindow都是nil.为什么?
可能重复:
在C中定义无参数函数main()的标准方法
我可以在C中使用函数的声明定义,main()如下所示:
int main() {}
Run Code Online (Sandbox Code Playgroud)
是的,我看到标准说只有两个保证支持的版本:
int main(void) {}
Run Code Online (Sandbox Code Playgroud)
和
int main(int argc, char* argv[]) {}
Run Code Online (Sandbox Code Playgroud)
但空洞的做法呢?我知道它有另外的含义而不是C++(在C中,它意味着这个函数的参数的数量和类型是未知的),但我在C中看到了很多带有main 声明定义的代码.
那谁错了?
为什么我不能使用reinterpret_cast运算符进行这样的演员表?
enum Foo { bar, baz };
void foo(Foo)
{
}
int main()
{
// foo(0); // error: invalid conversion from 'int' to 'Foo'
// foo(reinterpret_cast<Foo>(0)); // error: invalid cast from type 'int' to type 'Foo'
foo(static_cast<Foo>(0));
foo((Foo)0);
}
Run Code Online (Sandbox Code Playgroud) 如何放置UILabel在XCode 6的导航栏中心?有可能吗?例如,我可以放在这里,UIButton但无法放置UILabel.如果不是,我该怎么办?UIButton使用适当的文字放置一个并使其不可点击?
提前致谢.