I use VS2010 to generate an assembler listing. Than I create a new project, add generated *.asm file and try to compile it. During the compillation I receive some erros like:
main.asm(54): error A2008: syntax error : lambda0
at this line of generated asm:
PUBLIC ??R<lambda0>#?A0x1262112e##QBE_NH#Z ; `anonymous namespace'::<lambda0>::operator()
Another one is:
main.asm(72): error A2039: line too long
The length of 72th line is 538 symbols.
Looks like Visual Studio generates an incompatible asm code for examle for lambda expressions.
The next C++ code I used for test:
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cctype>
std::string LTrimString(std::string s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace((unsigned char)ch); }));
return s;
}
std::string RTrimString(std::string s)
{
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { return !std::isspace((unsigned char)ch); }).base(), s.end());
return s;
}
std::string TrimString(std::string s)
{
return LTrimString(RTrimString(s));
}
bool StartsWith(std::string full_str, std::string start_str)
{
return full_str.substr(0, start_str.length()) == start_str;
}
int main()
{
std::ifstream ifs("test.txt");
while(ifs)
{
std::string line;
std::getline(ifs, line);
if(StartsWith(TrimString(line), ">>>"))
std::cout << "###";
std::cout << line << '\n';
}
}
The question is: does it possible to generate correct asm file for compilation in visual studio or it can not be used for compilation?
Related
For an exercise i need to build a vector of polymorphic objects and for some reason both shared and Unique ptr make linkage errors 2019 and 1120 when i use them. i have no option to use the old way of memory allocation with New and Delete so i have to make it work. i tried various of different syntax options for doing this and still with no luck.
*note:
we use Cmake to bind the project together in visual studio
and also we splitting the objects into header and cpp files.
here are my objects for now:
//This is the abstract base class
#pragma once
#include <string>
class Shape
{
public:
Shape() = default;
virtual ~Shape() = default;
virtual std::string get_name() = 0;
private:
};
These are the derived classes:
#pragma once
#include "Shape.h"
class Circle : public Shape
{
public:
Circle();
~Circle();
virtual std::string get_name() override;
private:
std::string m_name;
};
Cpp file:
#pragma once
#include "Circle.h"
Circle::Circle()
: m_name("Circle")
{
}
Circle::~Circle()
{
}
std::string Circle::get_name() override
{
return m_name;
}
another derived class:
#pragma once
#include "Shape.h"
class Rectangle : public Shape
{
public:
Rectangle();
~Rectangle();
virtual std::string get_name() override;
private:
std::string m_name;
};
Cpp file:
#pragma once
#include "Rectangle.h"
Rectangle::Rectangle()
: m_name ("Rectangle")
{
}
Rectangle::~Rectangle()
{
}
std::string Rectangle::get_name() override
{
return m_name;
}
this is the class that operates the program:
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
class Board
{
public:
Board();
~Board();
void run();
void objects_allocation();
private:
std::string m_string;
std::vector<std::shared_ptr<Shape>> m_gates;
};
Cpp file:
#pragma once
#include "Board.h"
Board::Board()
: m_string(" ")
{
}
Board::~Board()
{
}
void run()
{
while (m_string != "exit")
{
std::cin >> m_string;
}
std::cout << "Goodbye!" << std::endl;
}
void Board::Objects_allocation()
{
m_gates.push_back(std::make_shared <Circle>());
m_gates.push_back(std::make_shared <Rectangle>());
}
and here is my main function:
#pragma once
#include "Board.h"
int main()
{
Board board1;
board1.run();
return 0;
}
sincerely thank you if you could explain to me what went wrong here..
The Problem was in the Cmake file. now everything is working just like i wanted.
__event T e(args);
On line 9, VS gives me green squigglies under 'e' with the warning: Function definition for 'e' not found.
On compile/build, it throws the C1001 internal error occurred in the compiler (line 9 again).
I've tried renaming the variable, tried removing the template and just working with normal types, tried making it public.
If anyone can help it would be greatly appreciated, thank you.
(Whole code probably not necessary but just to give an idea what I'm going for)
#include <cstdarg>
#include <functional>
#include <iostream>
template <typename T, typename ... args>
[event_source(native)]
class Action {
private:
__event T e(args);
public:
~Action() {
__unhook(this);
};
void operator +=(std::function<T(args...)> f) {
__hook(e, this, &f);
}
void operator -=(std::function<T(args...)> f) {
__unhook(e, this, &f);
}
void operator()(args...) {
__raise e(args);
}
};
void print(const char* s) {
std::cout << s << std::endl;
}
int main() {
Action<void, const char*> printAction;
printAction += print;
printAction("Print a string.");
printAction -= print;
}
I have problems to compile the following small code fragment. Visual Studio 2015 has problems in the deduction of the type of remover. Can someone explain me why and how to fix this error?
My idea was to create a reuseable function deleting the first occurence of a value in a given STL container under some user definable predicate.
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
#include <vector>
template<typename T>
auto removeOnlyOnce = [](std::vector<T>& v, const std::function<bool(const T&)>& pred) {
auto iter = std::find_if(v.begin(), v.end(), pred);
if (iter != v.end()) {
v.erase(iter);
}
};
int main(int argc, char** args) {
std::vector<int> v{ 1,2,3,4,2,2,3 };
// Works
std::function<bool(const int&)> isTwoPred = [](int x) { return x == 2; };
removeOnlyOnce<int>(v, isTwoPred);
// Also works
removeOnlyOnce<int>(v, [](const int x)->bool { return x == 2; });
// Gives compile error: C3538
// auto remover = std::bind(removeOnlyOnce<int>, v, std::placeholders::_1);
// remover([](const int x)->bool { return true; });
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}
The compiler issues the following warning:
main.cpp(26): error C3538: In einer Deklaratorliste muss "auto" immer in denselben Typ hergeleitet werden.
main.cpp(26): note: kann "<lambda_d00b55a1f2cac59f0efd4a81f45edea3>" sein
main.cpp(26): note: oder "std::_Binder<std::_Unforced,<lambda_d00b55a1f2cac59f0efd4a81f45edea3> &,std::vector<int,std::allocator<_Ty>> &,const std::_Ph<1> &>"
with
[
_Ty=int
]
Compiling with Wandbox and Option C++11 gives the following warning, which might be important:
prog.cc:8:6: warning: variable templates are a C++14 extension [-Wc++14-extensions]
auto removeOnlyOnce = [](std::vector<T>& v, const std::function<bool(const T&)>& pred) {
^
1 warning generated.
This lambda is not local. Please omit the & in [&] and you should be fine. Maybe a little cryptic, what I wrote: In other words, you don't have a this to capture.
Given a file that contains one integer per line, I am trying to use this code to read the file and store the numbers in a vector.
Strangely, after finishing reading the file, my program is printing the string "stoi". Is this just a behavior of std::stoi? I couldn't find anything about this in documentation.
I am using g++ 6.2.1.
Here is the relevant code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
void usage() {
std::cout << "Usage: ./binary_tree [FILE]\n";
}
int main(int argc, char* argv[]) {
try{
if (argc <= 1) {
usage();
return 1;
}
std::ifstream inputFile;
inputFile.open(argv[1], std::ios::in);
if (!inputFile.is_open()) throw std::runtime_error("Failed to open file");
std::string line;
std::vector<int> nums;
while(!inputFile.eof()) {
getline(inputFile, line);
int num = std::stoi(line);
nums.push_back(num);
}
// Clean up
inputFile.close();
return 0;
}
catch(const std::exception& e) {
std::cerr << e.what() << std::endl;
}
}
I am trying to parse a list input from the command line.
My class is derived from vector
The compiler complains about a overloaded validate being ambiguous.
I can see why, but do not know how to solve this issue.
Please help.
Below is a minimal example that generates the error. If the type of ch_list is changed to a vector, this minimal example compiles.
// g++ -std=c++11 -Wall -Wextra -pedantic test.cpp -o test -lboost_program_options -lboost_system
#include <vector>
#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
using namespace std;
class mylist : public vector<int> {
public:
friend istream &operator>>(istream &is, mylist& l) {
int val;
is >> val;
l.push_back(val);
return is;
}
friend ostream& operator<<(ostream& os, const mylist& l) {
return os << l[0];
}
};
int main (int argc, char* argv[])
{
//vector<int> ch_list; // This works
mylist ch_list; // This doesn't
namespace po = boost::program_options;
po::options_description desc("Allowed options");
desc.add_options()
("ch", po::value<decltype(ch_list)>(&ch_list), "Set channel numbers")
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error& e) {
cerr << "ERROR: " << e.what() << "\n";
return EXIT_FAILURE;
}
if (!ch_list.empty())
cout << ch_list[0] << "\n";
return EXIT_SUCCESS;
}
I get these errors
/usr/include/boost/program_options/detail/value_semantic.hpp: In instantiation of ‘void boost::program_options::typed_value<T, charT>::xparse(boost::any&, const std::vector<std::basic_string<charT> >&) const [with T = mylist; charT = char]’:
test.cpp:47:5: required from here
/usr/include/boost/program_options/detail/value_semantic.hpp:169:13: error: call of overloaded ‘validate(boost::any&, const std::vector<std::basic_string<char> >&, mylist*, int)’ is ambiguous
/usr/include/boost/program_options/detail/value_semantic.hpp:169:13: note: candidates are:
/usr/include/boost/program_options/detail/value_semantic.hpp:81:10: note: void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, T*, long int) [with T = mylist; charT = char]
/usr/include/boost/program_options/detail/value_semantic.hpp:129:10: note: void boost::program_options::validate(boost::any&, const std::vector<std::basic_string<charT> >&, std::vector<_RealType>*, int) [with T = int; charT = char]
You can use custom validator. Your code would be:
#include <vector>
#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
using namespace std;
namespace po = boost::program_options;
class mylist : public vector<int> {
public:
};
void validate(boost::any& v,
const vector<string>& values,
mylist*, int) {
mylist dvalues;
for(vector<string>::const_iterator it = values.begin();
it != values.end();
++it) {
stringstream ss(*it);
copy(istream_iterator<int>(ss), istream_iterator<int>(),
back_inserter(dvalues));
if(!ss.eof()) {
throw ("Invalid coordinate specification");
}
}
v = mylist(dvalues);
}
int main (int argc, char* argv[])
{
//vector<int> ch_list; // This works
mylist ch_list; // This doesn't
po::options_description desc("Allowed options");
desc.add_options()
("ch", po::value< mylist >(&ch_list)->multitoken(), "Set channel numbers")
;
po::variables_map vm;
try {
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);
}
catch (po::error& e) {
cerr << "ERROR: " << e.what() << "\n";
return EXIT_FAILURE;
}
for (auto cc : ch_list)
cout << cc << endl;
return EXIT_SUCCESS;
}
Reference: boost::program_options config file option with multiple tokens