我被以下代码严重咬了,我浪费了很多宝贵的时间.
#include<string>
int next(std::string param){
return 0;
}
void foo(){
next(std::string{ "abc" });
}
Run Code Online (Sandbox Code Playgroud)
这会产生以下编译器错误(在Visual Studio 2013上):
1>------ Build started: Project: sandbox, Configuration: Debug Win32 ------
1> test.cpp
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2039: 'iterator_category' : is not a member of 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1> c:\users\ray\dropbox\programming\c++\sandbox\test.cpp(8) : see reference to class template instantiation 'std::iterator_traits<std::basic_string<char,std::char_traits<char>,std::allocator<char>>>' being compiled
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C2146: syntax error : missing ';' before identifier 'iterator_category'
1>c:\program files (x86)\microsoft visual studio 12.0\vc\include\xutility(371): error C4430: …
Run Code Online (Sandbox Code Playgroud) 在MSVC 2012中的调试期间,我试图从Watch窗口调用一些函数,以便将数据转储到文件.但是,我不断收到此错误:
Function Matrix::Save has no address, possibly due to compiler optimizations.
Run Code Online (Sandbox Code Playgroud)
该类Matrix
位于我自己的外部库中.快速检查显示外部库中的所有方法都没有地址,并且所有从Watch调用它们的尝试都会返回此错误,但头文件中定义的除外.主项目中的方法都具有地址,无论它们在何处定义.
当然,在整个解决方案中禁用优化.符号正常加载.我如何解决它?
这是我的类型系统的简化版本:
#include <string>
#include <vector>
template<typename T>
class Box {
public:
Box(const T& value) : _value(value) {};
private:
T _value;
/* ... */
};
typedef Box<int> Int;
typedef Box<double> Double;
typedef Box<std::string> String;
int main(int argc, char* argv[]) {
String a("abc");
std::vector<String> b = { std::string("abc"), std::string("def") };
// error C2664: 'Box<std::string>::Box(const Box<std::string> &)' : cannot convert argument 1 from 'const char' to 'const std::string &'
std::vector<String> c = { "abc", "def" };
}
Run Code Online (Sandbox Code Playgroud)
虽然a
和b
编译,c …
使用VS2013 Update 2,我偶然发现了一些奇怪的错误消息:
// test.c
int main(void)
{
struct foo {
int i;
float f;
};
struct bar {
unsigned u;
struct foo foo;
double d;
};
struct foo some_foo = {
.i = 1,
.f = 2.0
};
struct bar some_bar = {
.u = 3,
// error C2440 : 'initializing' : cannot convert from 'foo' to 'int'
.foo = some_foo,
.d = 4.0
};
// Works fine
some_bar.foo = some_foo;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
GCC和Clang都接受了.
我错过了什么或这段代码是否暴露了编译器错误?
c designated-initializer compiler-bug visual-studio-2013 msvc12
我原以为这个静态断言会被激发:
#include <type_traits>
#include <memory>
int main() {
static_assert(std::is_copy_constructible<std::unique_ptr<int>>::value, "UPtr has copy constructor?");
}
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
使用MSVC12编译:
用于x64的Microsoft(R)C/C++优化编译器版本18.00.31101
这就是我所看到的:
1>------ Build started: Project: xxx (xxx\xxx), Configuration: Debug Win32 ------
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(64,5): error MSB4018: The "VCMessage" task failed unexpectedly.
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(64,5): error MSB4018: System.FormatException: Index (zero based) must be greater than or equal to zero and less than the size of the argument list.
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(64,5): error MSB4018: at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args)
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(64,5): error MSB4018: at System.String.Format(IFormatProvider provider, String format, Object[] args)
1>C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\Microsoft.Cpp.Platform.targets(64,5): error MSB4018: at Microsoft.Build.Shared.ResourceUtilities.FormatString(String unformatted, Object[] …
Run Code Online (Sandbox Code Playgroud) C++编译器GCC和MSVC之间的许多关键差异之一是,默认情况下,共享库中的所有符号都会被导出,而MSVC则不会导出任何符号.
一些含义是,在MSVC中,您必须导出explict实例化的模板类.
虽然我已经接受了这一点作为生活中的事实,但我想知道每种方法的设计含义,权衡,编译器设计者的角度等等是什么?
最近,我开始使用 MSVC 来编译一直使用 GCC 和 Clang 编译的代码。这段代码的某些部分会产生一个有趣的编译错误(被截断):
C:/data/msvc/14.33.31424-Pre/include\future(311) error C2678: binary '=': no operator found which takes a left-hand operand of type 'const Result' (or there is no acceptable conversion)
<source>(7): note: could be 'Result &Result::operator =(Result &&)'
<source>(7): note: or 'Result &Result::operator =(const Result &)'
Run Code Online (Sandbox Code Playgroud)
这段代码的最小示例:
#include <future>
#include <iostream>
struct Result {
int data{0};
};
// getting rid of constness resolves the problem for MSVC
using result_t = const Result;
using promise_result_t = std::promise<result_t>;
auto compute_result(promise_result_t …
Run Code Online (Sandbox Code Playgroud) 我有以下设置:
出乎意料的是:
Delphi命令行应用程序源代码:
program DelphiCpplibraryCall;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Windows;
type
TWork = function(Count : Integer) : Integer; cdecl;
var
Handle : THandle;
Work : TWork;
Result : Integer;
Freq : Int64;
Start : Int64;
Stop : Int64;
begin
try
Handle := LoadLibraryEx('worker.dll', 0, LOAD_WITH_ALTERED_SEARCH_PATH);
Work := GetProcAddress(Handle, 'work');
QueryPerformanceFrequency(Freq);
QueryPerformanceCounter(Start);
Result := Work(500000);
QueryPerformanceCounter(Stop);
Writeln(Format('Result: %d Time: %.6f s', [Result, (Stop-Start) …
Run Code Online (Sandbox Code Playgroud) 我记得过去人们vcvarsall.bat
在 Windows 上编译任何东西之前都会先运行一下。假设默认安装位置,我在哪里可以找到cl.exe
、link.exe
、 和朋友;如果它不在PATH
?
试图:
#include <stdlib.h>
#include <stdio.h>
#include <Windows.h>
const inline LPCWSTR find_cl() {
/* Most of this list derived from
https://gitlab.kitware.com/cmake/cmake/-/blob/417b765f/Modules/GetPrerequisites.cmake#L670 */
static const LPCSTR cl_paths[12] = {
"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\bin",
"C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin",
"C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\VC\\bin",
"C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\bin",
"C:\\Program Files\\Microsoft Visual Studio 9.0\\VC\\bin",
"C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\VC\\bin",
"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\BIN",
"C:\\Program …
Run Code Online (Sandbox Code Playgroud) msvc12 ×10
c++ ×7
visual-c++ ×5
c ×2
c++11 ×2
gcc ×2
clang ×1
compiler-bug ×1
constructor ×1
delphi ×1
dll ×1
performance ×1
type-traits ×1